2

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!

Tom Kidron
  • 177
  • 2
  • 2
  • 11

4 Answers4

1

You didn't close the first php conditional: if(isset($_POST['submit'])){". Close this with a} and your code seems good to go!

Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
Frank W.
  • 777
  • 3
  • 14
  • 33
1

You need to do two things:-

  1. close your first if statement in your action_page.php file.

  2. write:- <?php if(isset($fnameError)) {echo $fnameError;}?> and do the same for other two.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
0

There are two errors in your code:

Parse error: syntax error, unexpected end of file in the line

You have missed the closing braces of if(isset($_POST['submit'])) as other mate already mentioned.

Notice: Undefined variable: fnameError in C:\xampp\htdocs\practice\form.html on line 19

You are using PHP variable before initialize.

Solution:

  1. For ist point, add ending } before function start.
  2. For second point, move your PHP code before HTML.
devpro
  • 16,184
  • 3
  • 27
  • 38
0

1) As, I'm clearly seeing the error

"Notice: Undefined variable: fnameError in C:\xampp\htdocs\practice\form.html on line 19"

Means, you are having page name form.html So, Normally, you can only execute PHP code in a file which has .php extension because your webserver is setup like that for PHP.

Either

change form.html to form.php

Or

Create a file named .htaccess and place the following code in it and then place this file in your root directory for that website

AddType application/x-httpd-php .html

2) Put your php code before <form></form> for displaying the respective error.

3) Write

<span class="error">* <?php if(isset($fnameError)) {echo $fnameError;}?></span> 
<span class="error">* <?php if(isset($lnameError)) {echo $lnameError;}?></span>
<span class="error">* <?php if(isset($emailError)) {echo $emailError;}?></span>

for not getting

"Notice: Undefined variable:"

4) You didn't closed } for if(isset($_POST['submit'])){ . So, close it for not getting

Parse error: syntax error

Updated Code:

<?php

// 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
?>

<!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 if(isset($fnameError)) {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 if(isset($lnameError)) {echo $lnameError;}?></span>
                    <label>Email Address:</label>
                    <input type="email" name="emailaddress" id="email" placeholder="Email Address" value="" required><br>
                    <span class="error">* <?php if(isset($emailError)) {echo $emailError;}?></span>
                    <input type="submit" name="submit" id="submit" value="Submit">
                </form>
            </div>
        </div>
    </body>
</html>
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77