Home arrow PHP arrow Page 4 - Building a PHP 5 Form Processor: Using the Form Processor Package

Gluing the pieces: putting the form processing modules to work together - PHP

Welcome to the last part of the series “Building a PHP 5 form processor.” In three parts, this series goes through the development of an extensible form processor package in PHP 5, explaining its benefits and demonstrating its implementation.

  1. Building a PHP 5 Form Processor: Using the Form Processor Package
  2. Getting started: constructing programmatically an online form
  3. Performing server-side validation: implementing the “validator” module
  4. Gluing the pieces: putting the form processing modules to work together
  5. The complete application at a glance: listing the complete code of the form processor
By: Alejandro Gervasio
Rating: starstarstarstarstar / 22
January 30, 2006

print this article



Since you’ve already seen how each independent module works, next I’ll integrate them within the same PHP script. The end result of this will be a code fragment that displays the online form; after it’s been submitted, server validation is applied to each mandatory field. Take a closer look:

// if form was submitted perform server-side validation
    // instantiate form validator object
    $fv=new formValidator();
    $fv->validateEmpty('fname','Enter your first name (Min: 4
    $fv->validateEmpty('lname','Enter your last name (Min: 4
    $fv->validateEmail('email','Enter a valid email address');
    // check for errors
        // display errors
        echo '<h2>Resubmit the form after correcting the
following errors:</h2>';
        echo $fv->displayErrors();
    else {
        echo '<h2>Thank you for submitting your data!</h2>';
// create and display form
else {
        // instantiate form generator object
        $fg=new formGenerator();
        $fg->addFormPart('<tr><td>First Name</td><td>');
('name'=>'fname','required'=>array('empty','Enter your First name
(at least 4 characters)')));
        $fg->addFormPart('<tr><td>Last Name</td><td>');
('name'=>'lname','required'=>array('empty','Enter your Last name
(at least 4 characters)')));
('name'=>'email','required'=>array('email','Enter a valid email
('name'=>'send','value'=>'Send Data'));
        echo $fg->display();
    catch(Exception $e){
        echo $e->getMessage();

Here, notice how I use the “formValidator” class to check the validity of the data entered into required fields, after the form has been submitted. If any errors are found during the checking process, the warning messages are displayed on the page. Otherwise, the script confirms that submitted data is correct.

Similarly, the second part of the script constructs the corresponding form and displays it on the web page, by specifying the form elements to be rendered, as well as the fields on which client-side validation is performed. At this point, you’ll agree the overall script is very readable and each of their sections clearly shows what’s going on.

Now, the form processing package that I originally planned to build is completed. As you probably understand, with a wealth of classes behind the scenes charged with the hard work of rendering and processing forms, the use of this library demands only writing some lines of compact and readable code.

To provide you with the complete set of classes developed during the course of this series, the last section shows the source code for the entire form processor. So, let’s go there.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: