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 - Drop Down Control and Hidden Element (Page 9 of 13 )
<?php
// snip
// create a multiple select drop down $obj_registration_form->addElement('select', 'ddlLookingFor', 'Looking For:', array ("Publishing_Deal" => "Publishing Deal", "Label_Deal" => "Label Deal","Management_Deal" => "Management Deal", "Other" => "Other"), array("size" => "3", "multiple"));
// snip
// add a hidden value $obj_registration_form->addElement('hidden', 'txtReferrer', 'http://www.mysite.com');
// snip
?>
Next, I have added a multiple <SELECT> drop down control. This is pretty straightforward - just pass a value for the "size" attribute and the "multiple" keyword as elements of the array to the addElement() method.
This is followed by the definition of a "hidden" element - for the sake of completeness.
<?php
// snip
// creates a group of buttons to be displayed at the bottom of the form $obj_submit[] = &HTML_QuickForm::createElement('submit', 'btnSubmit', 'Register'); $obj_submit[] = &HTML_QuickForm::createElement('reset', 'btnReset', 'Start Again'); $obj_registration_form->addGroup($obj_submit, '', '', '  ');
// snip
?>
Above, I have grouped the two "button" controls in order to display them on a single line - once again using the ubiquitous "grouping" concept.
So far, I have manually added the different elements to a "group." However, the HTML_QuickForm() object also supports some controls that are "grouped" internally. The custom "date" element is one such example. This element is rendered by three drop down controls: one each for the day, month and year fields. The following statement will render an instance of the "date" element in a Web form:
<?php
// snip
$obj_registration_form->addElement('date', 'txtDateOfBirth', 'Date of Birth:');
// snip ?>
That was pretty simple, wasn't it?
Now, what’s next? Form validations, which is one concern that has yet to be addressed by the HTML_QuickForm() object. Not for long though, as you shall soon see in the following section.