| Form Processing |
| Article Index |
|---|
| Form Processing |
| Page 2 |
| Page 3 |
| Page 4 |
| Page 5 |
| Page 6 |
| Page 7 |
| Page 8 |
| Page 9 |
To accomplish all three of these goals, it took a little planning (all solid scripts do); however, in the end I think you'll agree that all three goals are met!
The form processor works through a combination of hidden form elements and dynamic function calls (discussed in , "Basic PHP Development"). These hidden form elements will be used by the PHP script to both provide a human-friendly description of each field element (in case of an error) and identify those form fields that are "required." Specifically, for any given form element with a name of <name>, the description of that field is defined as being stored in a hidden element by the name of <name>_desc. The example that follows defines a text field named "phone" with an extra description field for use within our script:
<INPUT TYPE="text" NAME="myphone">
<INPUT TYPE="hidden" NAME="myphone_desc" VALUE="Phone Number">
The second hidden form element that the form-processing script uses is called required and should contain a comma-separated list of required elements. For instance, if you have three required elements whose NAME attributes are phone, email, and fax, the hidden required tag would be as follows:
<INPUT TYPE="hidden" NAME="required" VALUE="phone,email,fax">
Although not a strict requirement, the VALUE attribute of each visible element should be populated with its associated value in the appropriate superglobal (that is, $_GET['myvar']) if available. This is done so that if the form is submitted and not processed for whatever reason (for example, an error) the user will not have to retype everything.
The next issue to be tackled is how to deal with validation errors that may occur when the form is submitted. In the form validation script, this is handled through two global PHP variables: $form_errors and $form_errorlist. When the form validation script attempts to validate the data submitted to it, upon an error, it creates these two variables. The first variable $form_errors is a Boolean value indicating whether an error occurred during validate, and the second $form_errorlist is an array of error messages that occurred during the form validation. How these variables are used in your script to display validation errors to the user is subjective; however, one recommended method is as follows:
<?php if($form_errors): /* An error occurred processing the form */ ?>
<UL>
<?php foreach($form_errorlist as $val): ?>
<LI><?php echo $val; ?>
<?php endforeach; ?>
</UL>
<?php endif; ?>
| Users' Comments (0) |
|
No comment posted









