Advertisement
Online Test Banks
Score higher
See Online Test Banks
eLearning
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

How to Validate Required Fields in a Form-Process Page

You build the PHP web form-process page in stages, starting with the basic framework and then adding more complex validation and features as you go.

  1. Open your text editor and create a new file.

  2. In that file, place the following code:

    <?php
     
    //prevent access if they haven't submitted the form.
    if (!isset($_POST['submit'])) {
        die(header("Location: form.php"));
    }
     
    session_start();
     
    $_SESSION['formAttempt'] = true;
     
    if (isset($_SESSION['error'])) {
        unset($_SESSION['error']);
    }
     
    $required = array("name","email","password1","password2");
     
    $_SESSION['error'] = array();
     
    //Check required fields
    foreach ($required as $requiredField) {
        if (!isset($_POST[$requiredField]) || $_POST[$requiredField] == "") {
            $_SESSION['error'][] = $requiredField . " is required.";
        } 
    }
     
    //final disposition
    if (isset($_SESSION['error']) && count($_SESSION['error']) > 0) {
        die(header("Location: form.php"));
    } else {
        unset($_SESSION['formAttempt']);
        die(header("Location: success.php"));
    }
    ?>
  3. Save the file as form-process.php in your document root.

  4. Load the main form.php file at http://localhost/form.php in your web browser.

  5. Click Submit Query without filling anything out in the form.

    image0.jpg

If you receive a page with the text fields colored red, then the JavaScript validation is still firing. Make sure you've commented out the JavaScript from form.php, and make sure the page has been reloaded recently in your browser.

Before continuing, look at this code since it serves as the basis for your PHP validation.

The first thing done in the file is to make sure it's being hit from the form's Submit button:

//prevent access if they haven't submitted the form.
if (!isset($_POST['submit'])) {
    die(header("Location: form.php"));
}

If that isn't the case, then the browser is redirected back to form.php.

Next up, the session is started and the formAttempt variable is set to true. Recall that this variable is used within the form.php page to indicate that the user has come from this process page versus reloading or using his or her Back button.

Next, all the existing errors are unset. There is no need for them in the process page, and you need to recheck everything again. The error array is initialized again.

if (isset($_SESSION['error'])) {
    unset($_SESSION['error']);
}
$_SESSION['error'] = array();

Next, an array is set up with the required fields. This makes adding required fields later an easy task. Just add them to this array:

$required = array("name","email","password1","password2");

The heart of the basic required field validation is next, inside a foreach loop:

//Check required fields
foreach ($required as $requiredField) {
    if (!isset($_POST[$requiredField]) || $_POST[$requiredField] == "") {
        $_SESSION['error'][] = $requiredField . " is required.";
    } 
}

If the field isn't set or is empty, then an error element is added to the $_SESSION['error'] array.

Finally, if the $_SESSION['error'] array has any elements, you need to redirect back to the form page; otherwise, send them to the success page.

//final disposition
if (count($_SESSION['error']) > 0) {
    die(header("Location: form.php"));
} else {
    unset($_SESSION['formAttempt']);
    die(header("Location: success.php"));
}
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win an iPad Mini. Enter to win now!