Graphic Design
  Home arrow Graphic Design arrow Page 2 - Using HTML Quickform for Form Processing
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? 
GRAPHIC DESIGN

Using HTML Quickform for Form Processing
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 160
    2004-09-01

    Table of Contents:
  • Using HTML Quickform for Form Processing
  • Steps for the Example
  • Individual Elements
  • Text, Password, Textarea
  • Hidden, Select
  • Checkbox, Radio
  • Submit, Reset, Button, Image
  • File, advcheckbox, Static
  • Header, Link, HTML
  • Element Groups
  • Processing Submitted Data
  • Without a Callback Function
  • Setting Validation Rules

  • 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 for Form Processing - Steps for the Example


    (Page 2 of 13 )

    The first step when using HTML_QuickForm is to load the code that defines the module with require or include. HTML_QuickForm is installed under your PEAR base directory at HTML/QuickForm.php.

    Next, you create a new HTML_QuickForm object. The HTML_Quickform() constructor takes one argument: the name of the form. This string is used as the value for the name and id attributes of the <form> tag in the page.

    The addElement() method adds a new element to the form. The order in which you add elements is the order in which they are displayed. The first argument to addElement() is the type of element you want to add. Valid element types for HTML_QuickForm are mostly the same as the valid values for type attribute of an HTML <input> tag: hidden, reset, checkbox, file, image, password, radio, button, submit, and text. Also valid are select and textarea, which correspond to the HTML <select> and <textarea> tags. HTML_QuickForm also has a few special element types of its own: hiddenselect, link, advcheckbox, date, static, header, and html. The next section covers all of these element types. This example form just uses three: text, select, and submit.

    The second argument to each addElement() call is the name of the form element. This string is used as the value for the name attribute of the HTML tag for the element. The third argument to addElement() is a label for the form element. Each element’s label is displayed next to the element. The call to addElement() that adds a select element to the form has a fourth argument: an array of choices to display as <option> tags within the select box. These lines add the title text box to the form:

    // Add a text box
    $form->addElement('text','title','Book Title:');

    These lines add the subject <select> menu, with three choices, to the form:

    // Add a select box
    $subjects = array('Math','Ice Fishing','Anatomy');
    $form->addElement('select','subject','Subject(s): ',$subjects);

    After the elements are added to the form, you call addRule() to add a validation rule. HTML_QuickForm includes many common validation rules, and you can write your own as well. The first argument to addRule() is the element to which the rule applies. The second argument is the error text to display when the rule is broken, and the third argument is the name of the rule. The required rule used in this example ensures that something is entered in the title text box.

    Once all of the elements and rules are defined, you can display or process the form as necessary. This example first calls the form’s validate() method. This returns true if form data has been submitted, and it passes all the rules defined by addRule(). It returns false if the submitted data fail one or more of the rules or if no data was submitted. If validate() succeeds, the process() method runs the specified form-processing callback function. If validate() fails, the display() method displays the form. The first time the page is loaded, the form is displayed because validate() fails when no form data is submitted. On subsequent submissions of the form, it is only redisplayed if the submitted data fails at least one rule. The process() method calls the praise_book() function and passes it one argument: an array of the submitted form data. The keys in this array correspond to the names of the form elements. The praise_book() function uses this array to display a message about the submitted book.

    The display() method lays out the form elements and their labels in a table. The HTML source code of the example form looks like this:

    <table border="0">
    <form action="qf-intro-example.php" method="post" name="book" id="book">
    <tr>
    <td align="right" valign="top"><font color="red">*</font><b>Book
    Title:</b></td>
    <td valign="top" align="left"><input name="title" type="text" /></td>
    </tr>
    <tr>
    <td align="right" valign="top"><b>Subject(s): </b></td>
    <td valign="top" align="left"><select name="subject">
    <option value="0">Math</option>
    <option value="1">Ice Fishing</option>
    <option value="2">Anatomy</option>
    </select></td>
    </tr>
    <tr>
    <td align="right" valign="top"><b></b></td>
    <td valign="top" align="left">
    <input name="save" value="Save Book" type="submit" />
    </td>
    </tr>
    <tr>
    <td></td>
    <td align="left" valign="top"><font size="1" color="#FF0000">*</font>
    <font size="1"> denotes required field</font>
    </td>
    </tr>
    </form>
    </table>

    By default, HTML_QuickForm uses the POST method and sets the action of the form to the value of $_SERVER['PHP_SELF']. You can override these settings by passing arguments to the HTML_QuickForm constructor. Remember that the first argument to the constructor is the form name. The second argument is the method to use. To create a form that uses the GET method, pass GET:

    $form = new HTML_QuickForm('myform','GET');

    The third argument to the constructor is an alternate URL to use as the form action:

    $form = new HTML_QuickForm('myform','POST','/store/purchase.php');

    To give the form a target attribute, which submits the form to a named frame or window, pass the frame or window name as a fourth argument to the constructor:

    $form = new HTML_QuickForm('myform','POST','/store/purchase.php','shopframe');

    If you want default values for the method and action URL, you can pass empty strings for those arguments:

    // use the default method (POST), but change the action URL
    $form = new HTML_QuickForm('myform','','/store/purchase.php');
    // submit the form to a new blank window, but use the default method and URL
    $form = new HTML_QuickForm('myform','','','_blank');

    To include arbitrary attribute/value pairs in the <form> tag, pass those as a fifth argument to the constructor. You can specify the attributes and values as a string:

    $form = new HTML_QuickForm('myform','','','','class="big" style="bold"');

    Or, you can specify the attributes as an array:

    $form = new HTML_QuickForm('myform','','','', array('class' => 'big', 'style' => 'bold'));

    This chapter is from Essential PHP Tools: Modules, Extensions, and Accelerators, by David Sklar, (Apress, 2004, ISBN: 1590592808). Check it out at your favorite bookstore today.

    Buy this book now.

    More Graphic Design Articles
    More By Apress Publishing


       · Any idea what version this comes in for the PEAR distribution?
       · If you follow the example code for uploading a file you may get frustrated that the...
       · Sorry,but I'd rather work in the old fashion way. You have much more control...
       · I think page 13 of the article, "Using HTML Quickform for Form Processing - Setting...
       · it's a really good tutorial. thank you again
       · Good intro, but you don't seem to have anything about setting default values, which...
       · Of course there is a method to set default values:e.g....
       · can you give me an example re: the client-side validation on custom rule.. the...
       · rangelength is not used as a string use array() for the 2 range numbers.ex, a 2...
     

    GRAPHIC DESIGN ARTICLES

    - Building Corner Effects with Transparent Bac...
    - 3D Graphics Technology: VRML Part I - Introd...
    - Creating Visual Effects
    - Web Page Design Overview
    - Creating Artistic Photographs
    - Working with Tools in Paint Shop Pro 8
    - Using HTML Quickform for Form Processing
    - Introduction to Adobe FrameMaker
    - WebLogic Workshop, WebLogic Platform, and th...
    - Planning the Site
    - Working with Web Services
    - WebLogic Workshop Application Development Ba...
    - Scanning Images for Web Use
    - Web Graphics Overview
    - The Pen is Mightier than the Brush Tool







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek