I'm trying to make a simple form that can check if the name and email are empty and not in the proper format
here's my code
<!DOCTYPE html>
<html>
<head>
<title>Register</title>
</head>
<body>
<?php
include ('connect.php');
$first = $_POST['fname'];
$last = $_POST['lname'];
$email = $_POST['email'];
if (isset($_POST['submit']))
{
if ($first == '' || $first == ' ' || $last == '' || $last == ' '|| $email == "" || $email == " " || !preg_match("/^[a-zA-Z]*$/", $first) || !preg_match("/^[a-zA-Z]*$/", $last) || !filter_var($email, FILTER_VALIDATE_EMAIL))
{
if ($first == '' || $first == ' ' )
{
$fnameErr = "First name is required!";
}
else
{
if(!preg_match("/^[a-zA-Z]*$/", $first))
{
$fnameErr = "Only letters are allowed!";
}
else
{
$fnameErr = "";
}
}
if($last == '' || $last == ' ')
{
$lnameErr = "Last name is required!";
}
else
{
if(!preg_match("/^[a-zA-Z]*$/", $last))
{
$lnameErr = "Only letters are allowed!";
}
else
{
$lnameErr = "";
}
}
if ($email == "" || $email == " ")
{
$emailErr = "Email is required!";
}
else
{
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$emailErr = "Invalid email!";
}
else
{
$emailErr = "";
}
}
}
else
{
echo "<br>You've successfully created your account<br>";
mysql_query("INSERT into register SET FNAME='$first', LNAME='$last', EMAIL='$email'")
or die(mysql_error());
}
}
?>
<h2 align="center">Registration Form</h2>
<form action="" method="POST" align='center'>
<p>First Name:
<input type="text" name="fname"> <?php echo $fnameErr; ?>
<br>
<p>Last Name:
<input type="text" name="lname"> <?php echo $lnameErr; ?>
<br>
<p>
Email:
<input type="text" name="email"> <?php echo $emailErr; ?>
<br><br>
</p>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>
I already declared a variables, but when I check it on my browser, it says:
Notice: Undefined variable: fnameErr in /opt/lampp/htdocs/eam/add.php on line 81
Notice: Undefined variable: lnameErr in /opt/lampp/htdocs/eam/add.php on line 84
Notice: Undefined variable: emailErr in /opt/lampp/htdocs/eam/add.php on line 89