Home arrow Graphic Design arrow Page 10 - Using HTML Quickform for Form Processing

Using HTML Quickform for Form Processing

With HTML_QuickForm version 3.1, you can have a consistent look across all your forms and a simplified file upload. This chapter starts with the basics then shows you how to process submitted form data with HTML_QuickForm. (From the book, Essential PHP Tools: Modules, Extensions, and Accelerators, by David Sklar, Apress, 2004, ISBN: 159059280.)

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 193
September 01, 2004
  1. · Using HTML Quickform for Form Processing
  2. · Steps for the Example
  3. · Individual Elements
  4. · Text, Password, Textarea
  5. · Hidden, Select
  6. · Checkbox, Radio
  7. · Submit, Reset, Button, Image
  8. · File, advcheckbox, Static
  9. · Header, Link, HTML
  10. · Element Groups
  11. · Processing Submitted Data
  12. · Without a Callback Function
  13. · Setting Validation Rules

print this article

Using HTML Quickform for Form Processing - Element Groups
(Page 10 of 13 )

You can present elements in groups with HTML_QuickForm. This changes their layout. Instead of each element having its own label and table row, all the elements in a group are displayed in the same table row. They share a label. To group elements, first create each of them using the createElement() method. Store the created elements in an array. Then, add the group of elements to the form by passing the array to the addGroup() method. For example, here’s how to create a group of three text fields:

$name[] = &HTML_QuickForm::createElement('text','firstname');
$name[] = &HTML_QuickForm::createElement('text','middlename'); $name[] = &HTML_QuickForm::createElement('text','lastname');
$form->addGroup($name,'user_name','Your Name: ',' ');

This adds the following HTML to the form:

<td align="right" valign="top"><b>Your Name: </b></td>
<td valign="top" align="left"><input name="user_name[firstname]" type="text"
/> <input name="user_name[middlename]" type="text" /> <input name="user_name[lastname]" type="text" /></td>

Each call to createElement() adds another element to the $name array. You can specify null for arguments such as label when you call createElement() because these elements take their label from the group.

The first argument to addGroup() is the array of elements that make up the group. The second argument is the name of the group, and the third argument is the single label used for the entire group. The individual element names in the HTML are constructed from the group name passed to addGroup() and the element name passed to createElement(). The group is treated as an array, and each text field is an element of that array. This results in individual element names of user_name[firstname], user_name[middlename], and user_name[lastname].

The <input type="text"> HTML tags for each element of the user_name group are separated by a space because the third argument to the addGroup() call is aspace. Specify any string to separate the elements by passing it to addGroup(). For phone numbers, a hyphen is a good separator:

$phone[] = &HTML_QuickForm::createElement('text','areacode',null,'size="3"');
$phone[] = &HTML_QuickForm::createElement('text','exchange',null,'size="3"');
$phone[] = &HTML_QuickForm::createElement('text','last4',null,'size="4"');
$form->addGroup($phone,'phone_number','Phone: ','-');

This group displays the following HTML:

 <td align="right" valign="top"><b>Phone: </b></td>
 <td valign="top" align="left"><input size="3" name="phone_number[areacode]" type="text" />-<input size="3" name="phone_number[exchange]" type="text" />-
<input size="4" name="phone_number[last4]" type="text" /></td>

In addition to using -as the element separator, this example passed attributes to createElement() as a fourth argument. These attributes adjust the size of each text field. The third argument, where a label would go, was left as the empty string. You can also use HTML as a group element separator. For example, make the group element separator to display a line break between each element in the group.

Grouping Radio Buttons

Creating a group of radio buttons with the same name but different values requires a slightly different syntax. The radio buttons should be created with no name or label, just text and value. For example, the following are three radio buttons in a group, each corresponding to one meal:

$meals[] =& HTML_QuickForm::createElement('radio',null,null,'Breakfast','br');
$meals[] =& HTML_QuickForm::createElement('radio',null,null,'Lunch','lu');
$meals[] =& HTML_QuickForm::createElement('radio',null,null,'Dinner','di');
$form->addGroup($meals,'meal','Meal: ','<br>');

Because each of the radio buttons in the group have no name of their own, they each take their name from the group name: meal. They have identical names but different values. Only one of the values is submitted with the form. For example, if the Breakfast radio button is selected, then the submitted form variable meal has the value br. The following is what the HTML looks like for this group of radio buttons:

<td align="right" valign="top"><b>Meal: </b></td>
<td valign="top" align="left"><input value="br" type="radio" id="qf_a8704d"
name="meal" /><label for="qf_a8704d">Breakfast</label><br>
<input value="lu" type="radio" id="qf_39a433" name="meal" /><label
<input value="di" type="radio" id="qf_c5fabd" name="meal" /><label

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.

blog comments powered by Disqus

- Customizing Wordpress Favicons and Gravatars
- 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

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials