Make your own free website on

Visit the Official PHP Website

Search this Site

PHP Programming

By James N Hitz

Unravelling the Mystery

Let me try to explain the code. While we step through the code, please remember that the variables being referred herein are created from the name attribute of the form widgets. Of course the entire PHP script is escaped from HTML by being enclosed within . That explains the first and last lines of the code:

The first if... elseif statement block tests two things.

  1. whether the name is blank (Remember we said the name is compulsory). If it is blank, the variable $missing is assigned the value "The first name has not been provided.
  2. if the first name is not blank, it is checked to ensure that it has AT LEAST one word character. Remember a word character (\w) falls in the range [A-Za-z0-9_]
 if(trim($fName) ==""){
   $missing =  "<p>The <b>First Name</b> has not been provided.";
 }elseif(!preg_match("/^\w+$/", $fName)){
   $missing .= "<p><b>First Name</b> contains invalid character(s)";

The second if block tests whether the name is NOT made up of ZERO or more word characters (notice the ! operator before preg_match(). This allows for nothing. This is because this field is optional. Should the name be there and be invalid the string "

The Last Name contains invalid character(s)" is appended to the variable $missing. Notice that to append we use .= and not =. Using the second form will assign the new value to the variable, erasing the first.

   $missing .= "<p>The <b>Last Name</b> contains invalid character(s)";

The third if block tests whether the email address supplied is valid. This validity is tested by checking whether the first part of one or more word characters at the beginning (^(\w+), followed by an @ symbol, another one or more word characters (\w+), a dot and then 2 or 3 word characters at the end - (\w){2,3}. This tests for an email address using the format: xxxxx@yyy.vv or xxxxx@yyy.www. Notice vv is 2 characters and www is three characters

Notice how the @ symbol and the dot are escaped - \@ and \. respectively. On error, an error message is appended to the $missing variable:

   $missing .= "<p>Invalid <b>Email Address</b> Provided.";

This code validation will disqualify any address containing more than one dots eg, and some of the new domain extensions with more than three characters. As a little exercise for you, why not create one, well-rounded regular expression that will test the email address a little more comprehensively. Drop me a line when you do and we (me and the world) will criticise it (positively, of course).

The final validation is the one that tests whether the suggestion field is blank. Remember this field is a required field. The validation is hereby done in the same way the name was tested - by first trimming off any trailing and leading spaces using the trim() function, and then testing to see whether the contents are equal to nothing.

 if(trim($suggestion) ==""){
    $missing .= "<p><b>Suggestion</b> is blank.";

The remaining part of the script tests to see if $missing is blank. If any errors were found then something was appended to $missing. If it is not blank the an error occurred somewhere. The contents of $missing are printed.

if(trim($missing) != ""){
    print "<p>There are a few problems with your data entry:</p>". $missing;

If all was well though, the user is thanked for their suggestions which are actually printed on the screen along with their name and email address.

 else {
    print "<h2>Thank You</h2>".
     "<p>Hello <b>". $fName . " ". $lName ."</b><p>Your suggestions state that".
     "<br><b>$suggestion</b><p>\. Your email address <b>". 
      $eAddress . "</b> is valid";

That is all the code does...

I will be seeing you in the next lesson. Right now I gotta go. Test out the code and at least me know what you think. I always test all my code before presenting it to the world. It is always possible that there is a point I did not put intomind when validating. If you think so, please drop me a line. Au revoir!

<< The Code | Back to the TOC >>

JamHitz Productions