0

I have a simple form on my website that I need to do server side validation on. If user enters incorrect data or leaves a field blank, the user should be re-shown the form with their submitted data pre-filled into the fields.

My form code looks like this

<!--Contact form-->
        <form name="contactform" action="validate.php" method="post">
            Name: 
            <input type="text" name="name" value=""><br>
            <span class="error">* <?php echo $nameError; ?></span><br>

            Email: 
            <input type="text" name="email" value="">
            <span class="error">* <?php echo $emailError; ?></span><br>

            Category:
            <select name="category">
                <option value="" disabled selected>Select</option>
                <option value="shipping">Sales</option>
                <option value="returns">Returns</option>
                <option value="shipping">Shipping</option>
                <option value="other">Other</option>
            </select><br>

            <textarea rows="10" cols="40" name="message" placeholder = "Send us a message"></textarea><br>
            <input type="submit" name="submit" value="Submit" onclick="validate()">

        </form>

At the moment all I'm testing to validate is the name and email fields. My problem is that when I load the page I immediately get errors saying "Notice: Undefined variable: nameError " which has to do with these lines in the code

<span class="error">* <?php echo $nameError; ?></span><br>  
<span class="error">* <?php echo $emailError; ?></span><br>

However I know the validations are working because I placed echo statements in my validate.php file to test this. If I submit the form with incorrect or correct data, it will take me to a blank validate.php page that just displays the echo statements

Here is my code for validate.php

## Initialise varialbes to null ##
$nameError ="";
$emailError ="";
$categoryError ="";
$messageError ="";

$name = $_POST['name'];

## On submitting form below function will execute ##
if(isset($_POST['submit'])){
if (empty($_POST["name"])) {
    $nameError = "Name is required";
    echo "name required";
    echo " ";
} else {
    //$name = test_input($_POST["name"]);
    //check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/", $name)) {
        $nameError ="Only letters and white space allowed";
        echo "Invalid name";
    }
}

if(empty($_POST["email"])) {
    $emailError = "Email is required";
    echo "email required";
} else {
    //$email = test_input($_POST["email"]);
    //check if email syntax is valid or not
    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]/", $email)) {
        $emailError = "You did not enter a vaild email";
        echo "invalid email";
    }
}
}

?>

So I know my validations are working, the problem is that they are not displaying on the form itself. Can anyone offer a solution on how to fix this? I've included a picture of one of the error messages I get when I load up the page Error message on page load

Eoin
  • 330
  • 1
  • 3
  • 15
  • So, u want the data to be filled automatically in ur fields, right.. to achieve it u can do like this.. Suppose `` is ur field.. and suppose this is ur php code.. `$fname=$_POST['fname'];` Now, `` will fill the field with the data which is entered by the user.. – phpfresher Apr 15 '15 at 13:04
  • Cool I will try your solution to that once I solve my other problem which is that at the moment when I submit the data it takes me to a blank validate.php (or one that echos out errors) instead of displaying them on the form itself. Have you any idea how to solve this? – Eoin Apr 15 '15 at 13:22
  • Once try keeping the code of `validate.php` in the `form.php` page.. I think its happening to you because the page is being redirected to `validate.php` and not staying on the `form.php`... – phpfresher Apr 16 '15 at 05:45

1 Answers1

1

I guess you don't fire validate.php on the initial page call. In this case $nameError is undefined. Add conditions checking if this variable is set into your form code.

Like this:

if ( isset( $nameError ) ) { echo $nameError; }
TheFrozenOne
  • 1,695
  • 9
  • 19
  • Thanks that got rid of the errors for me! The only problem now is that when I submit incorrect data it takes me to the validate.php page echos it out there instead of on the form itself? Is there a way to solve this? – Eoin Apr 15 '15 at 13:19
  • @Eoin Do you have the possibilities to include your validate-Code into your `form.php`? Then set the forms action to empty `action=""` it will then send the data to the same page. – TheFrozenOne Apr 15 '15 at 13:23
  • 1
    I managed to figure out the problem. Instead of having my PHP in a separate file I put it at the top of the Contact.php with the values set to null, so once the form is submitted the page calls itself again but this time with the values changed to the respective inputs, and ones with no inputs display the errors. – Eoin Apr 15 '15 at 16:16