Before we insert a new record in the database we want to make sure that the information is complete. A Boolean expression that tests all of the fields such as if(isset($firstName)) && isset(... is cumbersome and difficult to modify and test. Instead we will set $ok to true. Then we can test each field and if it is empty or invalid we can print a message and set $ok to false. When we finish checking each condition, if $ok is still true, then we can add the record to the database.
The following functions can help in validating the form:
empty($var): Returns true if the variable is considered empty. The following are considered empty: 0, "0", "", NULL, FALSE, an array that was declared but with no values. Note that " " is not considered empty, so we will trim the name first, then test for empty:
$firstName=trim($firstName);
if(empty($firstName)) {
$ok=false;
echo 'The first name can not be blank<BR>';
}
isset($var): Returns true if the variable has been given any value including 0 or "". If we are checking grades on a test, it is possible for a student to get a 0, so we would need to use isset rather than empty.
The date is more tricky. since we don't know what month will be selected, we don't know how many days to put in the day menu. The user can select a date such as September 31, or February 30. What we will do is make the input values for month, day and year into a date using the mktime function: $birthdate=mktime(0,0,0,$month,$day,$year); If the user entered Sept. 31, the mktime function makes the date Oct. 1st.
Next we will make a string using the input data: $format1="$month/$day/$year"; and also make a string by formatting the date: $format2=date("n/j/Y",$birthdate); The two strings should be the same, if not it is an invalid date.
Look at the second patient information form, make sure you test as many invalid conditions as you can think of. Look at the php code also to see how the validation was done.