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:
<tr>
<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>
</tr>
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:
<tr>
<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>
</tr>
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:
<tr>
<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
for="qf_39a433">Lunch</label><br>
<input value="di" type="radio" id="qf_c5fabd" name="meal" /><label
for="qf_c5fabd">Dinner</label></td>
</tr>
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. |
Next: Processing Submitted Data >>
More Graphic Design Articles
More By Apress Publishing