简体   繁体   中英

PHP single page form validation form not validating

Nothing happens when you click the submit button at the bottom of the page. I simply want it to validate user input and I am only focused on the name field at the moment and I cannot get it to validate any input in the name field. No error messages pop up or anything. Please review this and offer any suggestions, I cannot find my error.

PHP portion, where variables are initialized and set to empty. As well as the post methods and isset functions

<?php


//define variables and set them to empty values    

$fname_error= $phone_error= $address1_error= $address2_error= $city_error= $state_error= $zipcode_error= "";

$fname= $phone= $address1= $address2= $city= $state= $zipcode= "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["fname"])) {
        $fname_error = "Missing";
    }
    else {
        $fname = test_input($_POST["fname"]);
        //now we check to see that the name only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z ]*$/",$fname)) {
            $fname_error = "Please use letters and white space only";
        }
    }

function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

?>

The Html portion:

                               <div class="userinput">
                                    <label for="fname"><b>First Name</b></label>
                                    <input type="text" name="fname" value="<?php 
                                         echo $fname ?>">
                                    <span class="error">
                                        <?php echo $fname_error;?></span>
                                </div>

Good day. This is just a hypothesis, I may be wrong as I couldn't check the entire code, but you cannot have more than 1 form on the same page . Because, you need a single opening and closing form tag that wraps ALL form elements on your page. Form fields are only counted as part of a form if they are contained within the form elements. And you do have more than 1 form on the same page.

Also, you should consider minimizing your code to only what's needed.

Hope this helps!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM