The advantages of using validation on your forms means that you will gather all of the information, this is particularly useful if you need to populate a database with information, you need to make sure that the fields you require always get filled. As with everything in computing, there are many ways to achieve form validation, varying from client side Javascript to server side PHP and ASP etc. This tutorial will use PHP and server side processing to validate a form and output information about the process to the user.
What you will need:
1. Once you have created a basic page with html and body tags etc, name the page contact.php. The first step is to add the form to the page. Within the body tags add the following piece of code:
<form method="post" action="contact.php">
Name*:<br /><input type="text" name="name" /><br />
Email Address*:<br /><input type="text" name="email" /><br />
Phone Number:<br /><input type="text" name="phone" /><br />
Comments*:<br /><textarea name="comments"></textarea><br />
<input type="submit" value="Submit" />
<input type="hidden" name="submit_form" value="y" />
</form>
This will insert a basic contact form into a webpage where we want the Name, Email Address and Comments to be required inputs. This can be changed to suit your needs. You will notice a hidden input type, because its the same page which is called when you submit the form, this will test whether its a form submit or someone just coming to the page when the page is loaded.
2. The next step is to add the PHP code to process the form. Before anything on the page add the following piece of code:
<?php
if(isset($_POST['submit_form'])){
if(isset($_POST['name']) && isset($_POST['email']) && isset($_POST['comments'])){
// Code to execute if everything is fine
}else{
$error="incomplete";
}
}
?>
The first part of this code is testing to see if the page has been loaded from a form submit as discussed above. If it has been loaded from a form submit it then tests whether each of the required fields has been filled in, if they have then it will excute the code of your choice, whether it be an email or filling the contents into a database, this is a subject for another tutorial. If all of the fields havent been filled in then it sets an error variable to incomplete which we will use later.
3. The next step is to add the code to the form that will show the user which fields they havent filled in that are required. Add the following piece of code into the input tags of your form to make them turn red if the user has input the text (the following is an example for the email input, simply change the name to the field that the code is in):
style="<?php if(isset($_POST['submit_form']) && !isset($_POST['email'])){echo "background:#b12828;";} ?>"
To make the line:
Email Address*:<br /><input type="text" name="email" style="<?php if(isset($_POST['submit_form']) && !isset($_POST['email'])){echo "background:#b12828;";} ?>" />
4. We will now make use of the error variable to display a message to the user about why the form hasnt been successfully submitted. Above the form insert the following piece of code:
<?php if(isset($error)){echo "<p>Please complete all of the required fields</p>";} ?>
You can change the message that is echoed to anything you choose. If you advance the tutorial you may wish to add other errors such as invalid email address etc. If this is the case you may wish to change the if statement to a switch case function and add other error messages.
5. The final step is to add any information that has been filled in back into the fields to stop the user having to re-type information. To do this add the following piece of code into the input tags in the same way you did above (again this is just the example for the email field):
value="<?php echo $_POST['email']; ?>"
6. This tutorial only covers the basics of the self validating form, there are many improvements that you may wish to add including email address validation, a spam verifier, etc. If you feel like you have added something worthy of mention to your script then please let us know.
Display a Random Image with PHP
Self Validating Form with PHP
Using PHP Includes to your Advantage
Clean up your Website URLs with htaccess
Create a tag cloud for your site
Create an HDR Image from a Single Photo
Sharpen your Photos for Professional Results
Isolate a Colour in a Photo for a Striking Result
Digitally Colour a Black and White Photo
Using Adjustment layers in Photoshop
Using Blending Modes in Photoshop
Getting Pantone Values for use in Print