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.)
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:
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:
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:
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: