Order Form Part 9: Building The PHP Script

By Vince Barnes

So far our Order Form has involved a page built using HTML and containing our form, and some JavaScript to perform calculations and validations.  All the program code we have written so far (the JavaScript) runs on the client side, that is, it runs in the browser on the computer belonging to the site visitor.  Once the visitor has correctly filled out the form and hit the "Submit" button, we need to process their order.  This will happen on our computer, that is, on the web server computer where the site is hosted.

When it comes to server side scripting, PHP is a strong favorite.  It is pretty simple to learn (there's a tutorial series on our site here) and there are versions of it to run on most unix/linux flavors as well as Windows, making it one of the more ubiquitous server side languages.  It is the chosen language for our server side processing.  I'd like to send my thanks to my friend Mundi King, whose adeptship with PHP has me convinced that he can write complex PHP programs in minutes with both hands tied behind his back!

 To the script!  The first thing we'll be doing in our script is some validation.  We wrote some JavaScript (client-side) validation and if we did our job right, we should be getting nothing but valid data sent down to us.  However, there's no harm in checking again and the inclusion of some validation here will illustrate that it can be done server-side.  Here's the beginning of our script:

<?
//uncomment for debugging
//print_r($_POST);
 

//make sure there is data in the name and email fields
if( empty($_POST["Name"]) )
{
    $error["name"] = "Name is required.";
    $Name = "";
}
else
    $Name = $_POST["Name"];
 
if( empty($_POST["Email"]) )
{
    $error["email"] = "Email is required.";
    $Email = "";
}
else
    $Email = $_POST["Email"];
 
if( empty($_POST["OtherInfo"]) )
{
    $OtherInfo = "";
}
else
    $OtherInfo = $_POST["OtherInfo"];
 
 
 
//check to make sure the qty fields are whole numbers
//but only check if there was data entered
if( !empty($_POST["qtyA"]) )
{
    if( is_numeric($_POST["qtyA"]) && ( intval($_POST["qtyA"]) == floatval($_POST["qtyA"]) ) )
    {
        //we have a whole number
    }
    else
        $error["qtyA"] = "Please enter a whole number for Class A Widgets.";
}
 
if( !empty($_POST["qtyB"]) )
{
    if( is_numeric($_POST["qtyB"]) && ( intval($_POST["qtyB"]) == floatval($_POST["qtyB"]) ) )
    {
        //we have a whole number
    }
    else
        $error["qtyB"] = "Please enter a whole number for Class B Widgets.";
}
 
if( !empty($_POST["qtyC"]) )
{
    if( is_numeric($_POST["qtyC"]) && ( intval($_POST["qtyC"]) == floatval($_POST["qtyC"]) ) )
    {
        //we have a whole number
    }
    else
        $error["qtyC"] = "Please enter a whole number for Class C Widgets.";
}
 

//we should have at least 1 item ordered in the form
if( empty($_POST["qtyA"]) && empty($_POST["qtyB"]) && empty($_POST["qtyC"]) )
    $error["no_qty"] = "Please enter at least 1 item to order.";
 

if( is_array($error) )
{
 
    echo "An error occurred while processing your order.";
    echo "<br>\n";
    echo "Please check the following error messages carefully, then click back in your browser.";
    echo "<br>\n";
 
    while(list($key, $val) = each($error))
    {
        echo $val;
        echo "<br>\n";
    }
 
    //stop everything as we have errors and should not continue
    exit();
 
}
 

There's the start! now let's take a look at some of its components.  At the beginning of the script is a handy little debugging aid.  Uncommenting the code enables you to see the data that is being passed down to us -- handy if you can't otherwise tell what data your code is actually responding to.

As you look through each or the validation checks, the comments tell you what the code is doing.  This is a very sound programming practice -- it's a part of what I call "being your own best friend", and I highly recommend it.  You'll notice that each of the tests, if it fails, loads an error message into an array so that at the end of the tests if the array contains anything at all, the program can list all the errors in encountered and then stop, reminding the user that they can use the back button to get back to the form.

If no errors have been encountered. processing can continue.
 

 

Return to the previous step  |  Proceed to the next step



Make a Comment

Loading Comments...

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
  •  
  •  
  •  
Thanks for your registration, follow us on our social networks to keep up-to-date