使用php的电子邮件验证错误


Email validation error using php

我使用php进行了电子邮件验证,它显示了一些类似的错误

注意:未定义的变量:emailErr inC: 第36行上的''examplep''htdocs''ramesh_test''popup''email_validation.php

这是我的代码:

<?php
if(isset($_POST['submit']))
{
$emailErr ="";
$email="";
$email =$_POST["email"];
if(empty($email))
{
    $emailErr="must fill this feild";
}else{
     // check if e-mail address is well-formed
     if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
       $emailErr = "Invalid email format"; 
     }
     else
     {
     $emailErr = "Invalid"; }
     }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<form method="post" action="email_validation.php"> 
E-mail: <input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br>
<input type="submit" name="submit" value="Check" />
</form>
</body>
</html>

如果不提交表单,则不会设置表单变量$emailErr。因此,您需要检查是否设置了$emailErr

Change 

<?php echo $emailErr;?>

<?php echo isset($emailErr) ? $emailErr : '' ;?>