0

I have a simple PHP page, and am attempting to validate form input.

Upon hitting submit with invalid data, the inputted value is not being returned in my echo statement

I want to echo the input as the value so that the user can understand what they typed wrong. Below is my code;

Neither the echo of "TEST" . $contactEmail nor the input value are displaying $contactEmail

<?php
        // define variables and set to empty values
        $contactFirstNameErr = $contactEmailErr = $retailerIDErr = "";
        $contactFirstName = $contactEmail = $retailerID = "";

        if ($_SERVER["REQUEST_METHOD"] == "POST") {
        // collect value of input fields
            if (empty($_POST["contactFirstName"])) {
                $contactFirstNameErr = "<br>*First Name is required";
               } else {
                 $contactFirstName = test_input($_POST["contactFirstName"]);
                 // check if name only contains letters and whitespace
                 if (!preg_match("/^[a-zA-Z ]*$/",$contactFirstName)) {
                   $contactFirstNameErr = "<br>*Only letters and white space allowed"; 
                 }
               }
            //Email Field
            if (empty($_POST["contactEmail"])) {
                $contactEmailErr = "<br>*Email is required";
            } else {
                // check if e-mail address is well-formed
                if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                    $contactEmailErr = "<br>*Invalid email format"; 
                } else {
                    $contactEmail = test_input($_POST["contactEmail"]);
                }
            }
            //Option Field
            if (empty($_POST["retailerID"])) {
                $retailerIDErr = "<br>*Retailer is required";
            } else {
                $retailerID = test_input($_POST["retailerID"]);
            }
        }
    ?>

    <!--Begin HTML Form-->
    <div class="Form_container">
        <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
            Contact First Name<span class="required">*</span><span class="formError"><?php echo $contactFirstNameErr;?></span><br> <!--<p class='spacerLine'></p>-->
            <input type="text" class="largeInput" name="contactFirstName" value="<?php echo $contactFirstName;?>">
                <br><br>
            Contact E-mail<span class="required">*</span><span class="formError"> <?php echo $contactEmailErr;?></span><br>
            <input type="text" class="largeInput" name="contactEmail" value="<?php echo $contactEmail;?>">
                <br><br>
                <?php echo "TEST" . $contactEmail;?>
                <br><br>
            Retailer<span class="required">*</span><span class="formError"><?php echo $retailerIDErr;?></span><br>
            <input type="text" class="largeInput" name="retailerID" value="<?php echo $retailerID;?>">
                <br><br>
            <input type="submit"  class="button" name="submit" value="Add Contact"> 
        </form>
    </div>

Any thoughts? I'm new to PHP but have been following the W3 tutorial pretty tightly. Could it be my classes throwing things off? Or did I just mess up a variable name? Thanks for all help

gruff
  • 411
  • 1
  • 9
  • 25
  • 1
    As an aside, what have you got against people with names like *José* ? – CD001 Jan 18 '16 at 14:47
  • looks like `$contactEmail` is never set to a value other than the initial empty string. Try removing the `else` clause and setting `$contactEmail = test_input($_POST["contactEmail"]);` in all cases. – Luke Jan 18 '16 at 14:49
  • It also looks like test_input() function has not been defined anywhere? – Hexana Jan 18 '16 at 14:51

3 Answers3

0

The below line validates the value of the variable $email, but i can't see anywhere in your code where does that variable get set a value, that can be the first step in fixing the issue.

if (!filter_var($email, FILTER_VALIDATE_EMAIL))
Mahmoud Tantawy
  • 713
  • 5
  • 12
0

You are not defining test_input() function and $email is not defined in this line:

if (!filter_var($email, FILTER_VALIDATE_EMAIL))

This code works for me so far:

       $contactFirstNameErr = $contactEmailErr = $retailerIDErr = "";
    $contactFirstName = $contactEmail = $retailerID = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // collect value of input fields
        if (empty($_POST["contactFirstName"])) {
            $contactFirstNameErr = "<br>*First Name is required";
           } else {
             $contactFirstName = $_POST["contactFirstName"];
             // check if name only contains letters and whitespace
             if (!preg_match("/^[a-zA-Z ]*$/",$contactFirstName)) {
               $contactFirstNameErr = "<br>*Only letters and white space allowed"; 
             }
           }
        //Email Field
        if (empty($_POST["contactEmail"])) {
            $contactEmailErr = "<br>*Email is required";
        } else {
            // check if e-mail address is well-formed
            if (empty($_POST["contactEmail"])) {
                $contactEmailErr = "<br>*Invalid email format"; 
            } else {
                $contactEmail = $_POST["contactEmail"];
            }
        }
        //Option Field
        if (empty($_POST["retailerID"])) {
            $retailerIDErr = "<br>*Retailer is required";
        } else {
            $retailerID = $_POST["retailerID"];
        }
    }
Hexana
  • 1,095
  • 2
  • 12
  • 35
0

I want to echo the input as the value so that the user can understand what they typed wrong.

Neither the echo of "TEST" . $contactEmail nor the input value are displaying $contactEmail

First of all, echo $_POST values instead of $contactFirstName, $contactEmail etc. because these values are available only after it crosses all the validation steps.

Second, there's no function named test_input() in your code, or may be it is defined somewhere else.

And finally, look at this statement here:

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { ..

There's no variable named $email in your code. It should be:

if (!filter_var($_POST["contactEmail"], FILTER_VALIDATE_EMAIL)) { ..

So your code should be like this:

<?php

    function test_input($string){
        
        // your code
    }

    $contactFirstNameErr = $contactEmailErr = $retailerIDErr = "";
    $contactFirstName = $contactEmail = $retailerID = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        // collect value of input fields
        if (empty($_POST["contactFirstName"])) {
            $contactFirstNameErr = "<br>*First Name is required";
        } else {
            $contactFirstName = test_input($_POST["contactFirstName"]);
            // check if name only contains letters and whitespace
            if (!preg_match("/^[a-zA-Z ]*$/",$contactFirstName)) {
                $contactFirstNameErr = "<br>*Only letters and white space allowed"; 
            }
        }
        //Email Field
        if (empty($_POST["contactEmail"])) {
            $contactEmailErr = "<br>*Email is required";
        } else {
            // check if e-mail address is well-formed
            if (!filter_var($_POST["contactEmail"], FILTER_VALIDATE_EMAIL)) {
                $contactEmailErr = "<br>*Invalid email format"; 
            } else {
                $contactEmail = test_input($_POST["contactEmail"]);
            }
        }
        //Option Field
        if (empty($_POST["retailerID"])) {
            $retailerIDErr = "<br>*Retailer is required";
        } else {
            $retailerID = test_input($_POST["retailerID"]);
        }
    }
?>

<div class="Form_container">
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
        Contact First Name<span class="required">*</span><span class="formError"><?php echo $contactFirstNameErr; ?></span><br>
        <input type="text" class="largeInput" name="contactFirstName" value="<?php if(isset($_POST['contactFirstName'])){ echo $_POST['contactFirstName']; } ?>">
            <br><br>
        Contact E-mail<span class="required">*</span><span class="formError"> <?php echo $contactEmailErr;?></span><br>
        <input type="text" class="largeInput" name="contactEmail" value="<?php if(isset($_POST['contactEmail'])){ echo $_POST['contactEmail']; } ?>">
            <br><br>
            <?php 
                echo "TEST "; 
                if(isset($_POST['contactEmail'])){ echo $_POST['contactEmail']; }
            ?>
            <br><br>
        Retailer<span class="required">*</span><span class="formError"><?php echo $retailerIDErr;?></span><br>
        <input type="text" class="largeInput" name="retailerID" value="<?php if(isset($_POST['retailerID'])){ echo $_POST['retailerID']; } ?>">
            <br><br>
        <input type="submit"  class="button" name="submit" value="Add Contact"> 
    </form>
</div>

Here's the reference for isset() function:


Sidenote: Even though this answer will work you temporarily, but you should definitely look at how to strictly validate form inputs using regex.

Community
  • 1
  • 1
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37