Design Usability
  Home arrow Design Usability arrow Page 12 - Using HTML_QuickForm To Manage Web Forms, ...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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? 
DESIGN USABILITY

Using HTML_QuickForm To Manage Web Forms, Part 1
By: Harish Kamath
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 55
    2004-12-22

    Table of Contents:
  • Using HTML_QuickForm To Manage Web Forms, Part 1
  • Installing The HTML_QuickForm Package
  • My First HTML_QuickForm
  • FORM Elements
  • Adding Standard HMTL FORM Elements
  • Defining FORM Controls, Submitting Data
  • Grouping
  • "Checkbox" and "Radio" Elements
  • Drop Down Control and Hidden Element
  • Implementing Form Validations
  • Password Field Validation
  • Processing Data With HTML_QuickForm
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


    Using HTML_QuickForm To Manage Web Forms, Part 1 - Processing Data With HTML_QuickForm


    (Page 12 of 13 )

    Its all fine and dandy to have a class that streamlines the generation and rendering of a Web form as I have shown in this article, thus far. But all this effort amounts to nothing if I am unable to insert the data submitted by the user into a database. This is where the process() method of the HTML_QuickForm() object comes in handy.

    But before I list the code that demonstrates how to insert the data in a table, here is the structure of the "users" table used in the following example:

    mysql> desc users;
    +----------------------+---------------+------+-----+------------+-------+
    | Field                | Type          | Null | Key | Default    | Extra |
    +----------------------+---------------+------+-----+------------+-------+
    | fullname             | varchar(60)   |      |     |            |       |
    | address              | text          |      |     |            |       |
    | country              | varchar(60)   |      |     |            |       |
    | emailaddress         | varchar(100)  |      |     |            |       |
    | gender               | enum('F','M') |      |     | F          |       |
    | dateofbirth          | date          |      |     | 0000-00-00 |       |
    | group_name           | varchar(30)   |      |     |            |       |
    | group_type           | varchar(30)   |      |     |            |       |
    | looking_for          | varchar(255)  |      |     |            |       |
    | username             | varchar(10)   |      |     |            |       |
    | pword                | varchar(10)   |      |     |            |       |
    | newsletter_subscribe | enum('Y','N') |      |     | N          |       |
    | referrer_url         | varchar(255)  |      |     |            |       |
    +----------------------+---------------+------+-----+------------+-------+
    13 rows in set (0.05 sec)

    Here is the updated code listing:

    Code Listing 5

    Load this example in your browser and submit the form after filling valid data in all the fields. If all goes well, you should see the following "success.php" page:

    Using HTML_QuickForm

    Before explaining how the process() method works, I would like to highlight one more feature of the HTML_QuickForm() object - the ability to associate "filters" with your Web form. As the name suggests, this feature allows us to apply filters (in the form of pre-defined PHP functions) to all (or selected) elements of the Web form. One good example is the trim() function - it is a common practice to apply this function to remove leading and trailing white spaces that may be present in the data submitted via a Web form. Take a look at the next statement:

    <?php

    // snip

    // pre-validation filters come here
    $obj_registration_form->applyFilter('__ALL__', 'trim');

    // snip
    ?>

    This applyFilter() method takes two parameters. The first stores name of the element and the second represents the name of the PHP function to call. Note that I can use the keyword "__ALL__" if I wish to apply the filter to all elements of the Web form.

    It is also important to apply the filter at the right time. For example, you may want to delay applying some filters till the validation is complete, as we have done in the following statements:

    <?php

    // snip

    // validate form
    if($obj_registration_form->validate()) {
     
      // post-validation filters come here
      $obj_registration_form->applyFilter('__ALL__', 'addslashes');
      $obj_registration_form->applyFilter('__ALL__', 'htmlentities');
      $obj_registration_form->applyFilter('txtAddress', 'nl2br');
     
      // invoke the "store_user_info" function to store the user information
      $obj_registration_form->process('store_user_information', false);   
     
      // exit the script, on successful insertion
      header('Location: ./success.php');
    }

    // snip

    ?>

    Here, I have applied some filters only after the data has been validated, for a very simple reason: it is possible that PHP functions such as addslashes(), htmlentities() and nl2br() will alter the data submitted by the user and thereby cause one of the validation rules to fail, through no fault of the user.

    Coming back to the process() method of the HTML_QuickForm() object. In the code listing above, you'll notice that I have passed two parameters: the first represents the name of my "callback" function and the second parameter is set to "false" because I am not uploading any files to the server in my Web form.

    If you look at the beginning of the script, you will notice that I have defined a function called store_user_information() - this is my "callback" function and contains routine code to insert a record in the "users" MySQL table.


    <?php

    // snip

    // custom function to store user information into the database
    function store_user_information($ary_artist_info) {
     
    // store information in the database
      $connection = mysql_connect('localhost', 'guest', 'pass') or die ('Unable to connect!');
     
          mysql_select_db('db2') or die ('Unable to select database!');
         
          $query  = " INSERT INTO `users` ( `fullname` , `address` , `country` , `emailaddress` , `gender` , `dateofbirth` , `group_name` , `group_type` , `looking_for` , `username` , `pword` , `newsletter_subscribe`, `referrer_url` )";

          $query .= " VALUES ('".implode(" ", $ary_artist_info['txtFullName'])."', '".$ary_artist_info['txtAddress']."', '".$ary_artist_info['ddlCountry']."', '".$ary_artist_info['txtEmailAddress']."', '".$ary_artist_info['radGender']."', '".implode("-", $ary_artist_info['txtDateOfBirth'])."', '".$ary_artist_info['txtGroupName']."', '".$ary_artist_info['radGroupType']."', '".implode(",", $ary_artist_info['ddlLookingFor'])."', '".$ary_artist_info['txtUsername']."', '".$ary_artist_info['txtPassword1']."', '".(isset($ary_artist_info['chkNewsletter']) ? "Y" : "N")."', '".$ary_artist_info['txtReferrer']."');";
        
          $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
       
          mysql_close($connection);

    }

    // snip

    ?>

    If all goes well, the data submitted by the user will be inserted into the database and he (or she) will be redirected to a "success.php" page.

    With that, I can also safely conclude that I am ready to roll out my "Registration" prototype - the task that was made easy by the resourceful HTML_QuickForm package.

    More Design Usability Articles
    More By Harish Kamath


       · When first time i saw this article i thought that it will solve all my problems that...
       · Then install the pear common package.
       · Great article! Where and how does the $ary_artist_info array get created in this...
     

    DESIGN USABILITY ARTICLES

    - Create Great JavaScript and CSS Menus Simply
    - Design Principles that Shape a Web Site
    - Creating Aqua Style Images
    - Easy as A,B,C – dynamic A to Z indexes
    - EasyChart: a Usability Teaching Tool to Demo...
    - Building Friendly Pop-up Windows
    - Back to School: Design Usability
    - Using HTML_QuickForm To Manage Web Forms, Pa...
    - Using HTML_QuickForm To Manage Web Forms, Pa...
    - More Website Knick Knack
    - Browsers as Test Platforms
    - Website Knick Knack
    - Dynamic Page Elements-Cloak and Dagger Web D...
    - Accessibility and Dreamweaver MX 2004







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