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

时间:2015-04-07 04:01:58

标签: php email-validation

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

  

注意:未定义的变量:emailErr in   第36行的C:\ xampp \ 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>

1 个答案:

答案 0 :(得分:1)

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

Change 

<?php echo $emailErr;?>

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