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

Email validation error using php

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

Notice: Undefined variable: emailErr in C:\xampp\htdocs\ramesh_test\popup\email_validation.php on line 36

这是我的代码:

<?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 : '' ;?>