This section introduces this great package, demonstrating
some of its most useful features.
Installing HTML_QuickForm
To take advantage of HTML_QuickForm??™s features, you need to install it from PEAR. Because
it depends on HTML_Common, another PEAR package capable of displaying and manipulating
HTML code, you need to install HTML_Common also, which is done automatically
by passing the --onlyreqdeps flag to the install command:
%>pear install --onlyreqdeps HTML_QuickForm
downloading HTML_QuickForm-3.2.7.tgz ...
Starting to download HTML_QuickForm-3.2.7.tgz (102,475 bytes)
........................done: 102,475 bytes
downloading HTML_Common-1.2.3.tgz ...
Starting to download HTML_Common-1.2.3.tgz (4,746 bytes)
...done: 4,746 bytes
install ok: channel://pear.php.net/HTML_Common-1.2.3
install ok: channel://pear.php.net/HTML_QuickForm-3.2.7
Creating a Simple Form
Creating a form is a breeze using HTML_QuickForm; just instantiate the HTML_QuickForm
class and call the addElement() method as necessary, passing in the element types
and attributes to create each form component. Finally, call the display() method to
render the form. Listing 13-2 creates the form displayed in Figure 13-1.
Listing 13-2. Creating a Form with HTML_QuickForm
require_once "HTML/QuickForm.php";
CHAPTER 13 ?– FORMS 357
// Create array of languages to be used in multiple-select box
$languages = array(
'C#' => 'C#',
'JavaScript' => 'JavaScript',
'Perl' => 'Perl',
'PHP' => 'PHP'
);
// Instantiate the HTML_QuickForm class
$form = new HTML_QuickForm("languages");
// Add text input element for entering username
$form->addElement('text', 'username', 'Your name: ',
array('size' => 20, 'maxlength' => 40));
// Add text input element for entering e-mail address
$form->addElement('text', 'email', 'E-mail address: ',
array('size' => 20, 'maxlength' => 50));
// Add select box element for choosing favorite programming languages
$select =& $form->addElement('select', 'languages',
'Your favorite
programming languages: ', $languages);
// Assign the multiple attribute to select box
$select->setMultiple(1);
// Add submit button
$form->addElement('submit', null, 'Submit!');
// Display the form
$form->display();
?>
But creating and displaying the form is only half the battle; you must always validate
and then process the submitted data.
Pages:
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433