Tired of authoring dull and dreary Web forms everyday? Fed up of programming JavaScript validations day-in and day-out? Then, the HTML_QuickForm package is just the solution for which you are looking. In the first part of this two-part tutorial, I'll get you started with the basics of building Web forms using this resourceful PEAR package.
Using HTML_QuickForm To Manage Web Forms, Part 1 - Grouping (Page 7 of 13 )
This next example continues from where the first one left off. It will demonstrate the unique ability of the HTML_QuickForm() to "group" Web form controls together.
Load this example in your browser to see the following output:
While the above output does reflect the interface changes, it does not throw any light on the interesting concept of "grouping" Web form elements. To learn more about the latter, you will need to analyze the code behind the scenes. Take a look at the following code snippet:
<?php
// snip
// Creates a group of text inputs $obj_first_name = &HTML_QuickForm::createElement('text', '', '', array('size' => 30, 'maxlength' => 30)); $obj_last_name = &HTML_QuickForm::createElement('text', '', '', array('size' => 30, 'maxlength' => 30)); $obj_registration_form->addGroup(array($obj_first_name, $obj_last_name), 'txtFullName', 'Full Name:', ' ');
// snip
?>
In the first example, I provided a single textbox for the "Full Name" field whereas most Web forms provide two text boxes, one each for the "First Name" and "Last Name" fields. This anomaly needs to be corrected. This is where the ability of the HTML_QuickForm() object to group <FORM> elements and treat them a single entity is useful.
The "grouping" of the "First Name" and "Last Name" fields is one possible example. Another common example is a Web form field that accepts a credit card number. The user can be requested to enter it in groups of four numbers as embossed on the credit card itself.
Coming back to the script, I have invoked the createElement() static method in order to create an instance of the HTML_QuickForm_element() object. The parameters of this static method mimic those of the addElement() method and the nature of element object created depends on the "keyword" value passed to the method.
Once I have defined all the elements of a particular "group," I invoke the addGroup() method of the core HTML_QuickForm() object and pass an array, containing the element instances, along with a "name" and a "label" for the "grouped" control.
The fourth parameter represents the separator used between the elements to be grouped - I have passed a non-breaking space entity character in order to display the two text fields on the same line.