I'm having a N00bish problem, working on a simple form validation.
I've created a form (html), which should perform a simple data validation, when submitting it to a seperate php file.
The html contains a placeholder for the error message, which is defined in the php file.
However, when i load the form (before submitting it), i'm getting the dreaded "Notice: Undefined variable: fnameError in C:\xampp\htdocs\practice\form.html on line 19" error.
But the variable is declared and initialized on the php file...
I've also tried to add a reference to the php file in the html, using "include".
However, i then get a different error message: "Parse error: syntax error, unexpected end of file in the line"
The html file contains the following code:
<!DOCTYPE html>
<html>
<head>
<title>Form Validation</title>
</head>
<body>
<div class="maindiv">
<div class="form_div">
<div class="title">
<h2>Form validation with PHP</h2>
</div>
<form action="action_page.php" method="post" name="form_name" id="form_id" class="form_class">
<h3>Form:</h3>
<span class="error">* required field.</span><br>
<label>First Name:</label>
<input type="text" name="firstname" id="fname" placeholder="First Name" value="" pattern="[a-zA-Z]+" required><br>
<span class="error">* <?php echo $fnameError;?></span>
<label>Last Name:</label>
<input type="text" name="lastname" id="lname" placeholder="Last Name" value="" pattern="[a-zA-Z]+" required><br>
<span class="error">* <?php echo $lnameError;?></span>
<label>Email Address:</label>
<input type="email" name="emailaddress" id="email" placeholder="Email Address" value="" required><br>
<span class="error">* <?php echo $emailError;?></span>
<input type="submit" name="submit" id="submit" value="Submit">
</form>
</div>
</div>
</body>
</html>
And the PHP file contains the following code:
<?php
// Initialize variables to null.
$fnameError = "";
$lnameError = "";
$emailError = "";
// On submitting the form, the below functions will execute.
if(isset($_POST['submit'])){
if(empty($_POST["firstname"])){
$fnameError = "First name is required";
} else {
$fname = test_input($_POST["firstname"]);
// Check firstname contains only letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$fname)) {
$fnameError = "Only letters and whitespace allowed";
}
}
if(empty($_POST["lastname"])){
$lnameError = "Last name is required";
} else {
$lname = test_input($_POST["lastname"]);
// Check firstname contains only letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$lname)) {
$lnameError = "Only letters and whitespace allowed";
}
}
if (empty($_POST["emailaddress"])) {
$emailError = "Email is required";
} else {
$email = test_input($_POST["emailaddress"]);
// check if e-mail address syntax is valid or not
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
$emailError = "Invalid email format";
}
}
function test_input($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
//php code ends here
?>
What am i doing wrong here?
Thanks!