Erro de validação do E-mail com o php

fiz a validação de E-mail usando o php, mostrando algum erro como este

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

e este é o meu código:

<?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>
Author: Leandro Papasidero, 2015-04-07

1 answers

Quando não apresentar a sua variável do formulário $emailErr não está definida. Então você precisa verificar se $emailErr set ou não.

Change 

<?php echo $emailErr;?>

A

<?php echo isset($emailErr) ? $emailErr : '' ;?>
 1
Author: Awlad Liton, 2015-04-07 04:03:55