Online
 
Friday, 21 November 2008
 
 

Form Processing | Print |  E-Mail
 

Anytime you are working with HTML forms, using some method or another you have to deal with processing that form. Often, not only does the form have to be processed (meaning you have to do something with the form data), but it almost always has to be validated in some fashion. In fact, it is strongly recommended that all form data is validated prior to being used in your scripts.

Basic Form Processing and Validation

In the simplest sense, form validation and processing is nothing more than working with the appropriate superglobal array ($_GET or $_POST) to do something in your PHP script. However, for a form of any complexity, often a considerable amount more goes into the validation of the data. As previously stated, it is simply bad practice (and dangerous) to use user-submitted data without properly validating it. For anything beyond the most elementary validation, usually all form validation is done via regular expressions, as shown in :

Listing 5.6. Elementary Form Validation
<?php
if(isset($_GET['submit'])) {
if(preg_match("/^\(([2-9][0-9]{2})\)[2-9][0-9]{2}-[0-9]{4}$/i",
$_GET['phone']) != 1) {
echo "The phone field was invalid<BR>";
}
} else {
/* Code to process form here */
}
?>
<HTML>
<HEAD><TITLE>Elementary form validation</TITLE></HEAD>
<BODY>
<FORM ACTION="<?php echo $_SERVER['PHP_SELF']; ?>" METHOD=GET>
<INPUT TYPE="hidden" NAME="submit" VALUE="1">
Phone: <INPUT TYPE="text" NAME="phone" SIZE=13 MAXLENGTH=13>
(ex. (810)555-1212)<BR>
<INPUT TYPE="submit" VALUE="Send">
</FORM>
</HTML>

Because form validation is such an application-specific subject (every situation is different), there is little benefit to discussing more about the general validation and processing of forms. Instead, we'll try to kill multiple birds with one stone by creating a form-processing and validation architecture that is general enough to use on any form without sacrificing flexibility. Be forewarned that to create a processing script that is both flexible and easy to use requires some fancy PHP programming! Don't worry too much, however; I will be explaining the script extensively.

General-Purpose Form Validation

Before I actually discuss a line of code, let me first explain the concept behind this all-purpose form-validation script. When I set out to create this script, I had the following goals in mind:

  • Be flexible enough to validate and process any form data.

  • Encourage the separation of validation-related code from presentation-related code.

  • Most of all, be easy enough to use when implementing any type of HTML form.

 

 

 

 



This entry was posted on . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a comment. Tags: Simple PHP, Pear, Easy PHP, PHP Tutorial, PHP MySQL, XSLT, Sap Tutorial, CSS Tutorial, XSL FO Java, SQL Tutorial.
Users' Comments (0)

Comment an article
  Name
  E-mail
   Title
Available characters: 4000
 Notify me of follow-up comments
This image contains a scrambled text, it is using a combination of colors, font size, background, angle in order to disallow computer to automate reading. You will have to reproduce it to post on my homepage
Enter what you see:

No comment posted

Rokok Kretek Jumbo Coklat

“Terbuat dari tembakau, saos dan cengkeh pilihan kwalitas tinggi sehingga menghasilkan rokok dengan rasa yang cocok untuk segala cuaca”
Jumbo Coklat - Powered By G-Ads

 
Top! Top!