PHP
  Home arrow PHP arrow Page 4 - Building a PHP 5 Form Processor: Using the Form Processor Package
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Building a PHP 5 Form Processor: Using the Form Processor Package
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 21
    2006-01-30


    Table of Contents:
  • Building a PHP 5 Form Processor: Using the Form Processor Package
  • Getting started: constructing programmatically an online form
  • Performing server-side validation: implementing the “validator” module
  • Gluing the pieces: putting the form processing modules to work together
  • The complete application at a glance: listing the complete code of the form processor

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Building a PHP 5 Form Processor: Using the Form Processor Package - Gluing the pieces: putting the form processing modules to work together
    ( Page 4 of 5 )

    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
    if($_POST['send']){
        // instantiate form validator object
        $fv=new formValidator();
        $fv->validateEmpty('fname','Enter your first name (Min: 4
    characters)');
        $fv->validateEmpty('lname','Enter your last name (Min: 4
    characters)');
        $fv->validateEmail('email','Enter a valid email address');
        // check for errors
        if($fv->checkErrors()){
            // 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 {
        try{
            // instantiate form generator object
            $fg=new formGenerator();
            $fg->addFormPart('<table>');
            $fg->addFormPart('<tr><td>First Name</td><td>');
            $fg->addElement('text',array
    ('name'=>'fname','required'=>array('empty','Enter your First name
    (at least 4 characters)')));
            $fg->addFormPart('</tr>');
            $fg->addFormPart('<tr><td>Last Name</td><td>');
            $fg->addElement('text',array
    ('name'=>'lname','required'=>array('empty','Enter your Last name
    (at least 4 characters)')));
            $fg->addFormPart('</tr>');
            $fg->addFormPart('<tr><td>Email</td><td>');
            $fg->addElement('text',array
    ('name'=>'email','required'=>array('email','Enter a valid email
    address')));
            $fg->addFormPart('</tr>');
            $fg->addFormPart('<tr><td>Comments</td><td>');
            $fg->addElement('textarea',array
    ('name'=>'comments','rows'=>'10','cols'=>'20'));
            $fg->addFormPart('</tr>');
            $fg->addFormPart('<tr><td>&nbsp;</td><td>');
            $fg->addElement('submit',array
    ('name'=>'send','value'=>'Send Data'));
            $fg->addFormPart('</tr></table>');
            echo $fg->display();
        }
        catch(Exception $e){
            echo $e->getMessage();
            exit();
        }
    }

    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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT