You Are Here: Home »Tutorials»Php_mysql »   Error checking forms Saturday October 11th 2008

Error Checking In Forms

Introduction

I guess this tutorial really covers two things in one, error checking when creating forms, checking for blank fields and checking if two fields match each other, but i guess you could also call it part four of the guestbook tutorial because much of it you can apply to the form in your guestbook (if you made one from the tutorial) that people use to add messages.

Checking For Blank Fields

One of the most common things that bored people like to do is see if they can submit your contact form or whatever else it is without filling out all the boxes, though also if you have a form with a number of fields sometimes people just miss one unintentionally.

An example form:

<form name="contact" method="post" action="sendform.php">
<table cellpadding="4" cellspacing="0">
 <tr>
  <td>Name:</td>
  <td><input type="text" name="name" /></td>
 </tr>

 <tr>
  <td>Email:</td>
  <td><input type="text" name="email" /></td>
 </tr>

 <tr>
  <td>Message:</td>
  <td><textarea name="message" cols="20" rows="6"></textarea></td>
 </tr>

 <tr>
  <td> </td>
  <td><input type="submit" name="submit" value="Send!" /></td>
 </tr>
</table>
</form>

Lets say your file 'sendform.php' has the following:

<?php

if($_POST['submit'])
{
   
$to 'you@yourmail.com';
   
$subject 'Your Subject';
   
            @
extract($_POST);
            
mail($to,$subject,$message,"From: $name <$email>");
}

?>

This would send the email but, if people hit submit without filling out the fields it'll send a blank email which can be annoying so lets try and stop it.

The most simple format to check if a string is blank would be with a basic if statement

if($_POST['$name'] == ''# $name value is blank

So we're looking to see if the "name" field is empty, if it is we probally want to return an error massage to the user. As we have more than just the $name value that we want to check though, we need a way to do the same for each required field in the form, a simple way would be with a basic if then else statement:

if($_POST['$name'] == '') {
            print 
"Please complete the name field\n";
}
elseif(
$_POST['$email'] == '') {
            print 
"Please complete the email field\n";
}
elseif(
$_POST['$message'] == '') {
            print 
"Please complete the message field\n";
}

And so on for the rest of the fields you want to check, though this wouldn't be the most efficient way, not only will it result in alot of code if you have many fields to check, if the user leaves more then one blank it'll only return an error for the first that it finds.

A slightly better way would be to loop through each of the fields you have checking them one by one and telling the user about all fields they might have missed:

$fields = array('name',
                
'email',
                
'message');

foreach(
$fields as $checked_field)
{
if(
$_POST['$checked_field'] == '')
{
         print 
"please fill out the $checked_field<br />\n";
    }
}

So now if somebody submits the form with any blank fields they will be given error message for each that they missed.

Checking a Match

Secondly we'll look at checking if two fields are the same. This is probally not something you will use often but it's useful if mabye you want to provide two fields for an email address, one for people to enter the address and one for them to confirm it. Or if you're asking people to create a password, you would want to ask them to input it twice and then check if they are the same, this can be done with a simple comparrison operator.

Say we have two inputs for an email in our form:

<input type="text" name="email" />
<input type="text" name="conf_email" />

You can then compare the value of both quite easily with:

if($_POST['email'] == $_POST['conf_email']) # the fields match

And you can use the same format for any fields where you want to check if the user has put the same into both. Or instead use the operator != to check if the fields do not match:

if($_POST['email'] != $_POST['conf_email']) # the fields do not match

That's all for now, some basic error checking for your forms, and if you followed the guestbook tutorial you can apply some of it to the form people use to add messages.