O'Reilly logo

Learning PHP 5 by David Sklar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The HTML_QuickForm Form-Handling Framework

Chapter 6 provides all the building blocks of robust form handling. A PEAR module, HTML_QuickForm, takes things a step further. It makes it easy to use common validation rules and simplifies default processing and encoding user input with htmlentities( ) . With HTML_QuickForm, the entire form is an object. You call methods on that object to add elements and validation rules to the form. Example 13-9 uses HTML_QuickForm to build the form in Example 6-30.

Example 13-9. Building a form with QuickForm

<?php // Load the QuickForm library require 'HTML/QuickForm.php'; // Create the form object $form = new HTML_QuickForm( ); // Define the same arrays of valid sweets and main dishes $sweets = array('puff' => 'Sesame Seed Puff', 'square' => 'Coconut Milk Gelatin Square', 'cake' => 'Brown Sugar Cake', 'ricemeat' => 'Sweet Rice and Meat'); $main_dishes = array('cuke' => 'Braised Sea Cucumber', 'stomach' => "Sauteed Pig's Stomach", 'tripe' => 'Sauteed Tripe with Wine Sauce', 'taro' => 'Stewed Pork with Taro', 'giblets' => 'Baked Giblets with Salt', 'abalone' => 'Abalone with Marrow and Duck Feet'); // Set the default values for form elements $form->setDefaults(array('delivery' => 'yes', 'size' => 'medium')); // Add each element to the form $form->addElement('text','name','Your Name: '); $form->addElement('radio','size','Size:','Small', 'small'); $form->addElement('radio','size','', 'Medium', 'medium'); $form->addElement('radio','size','', 'Large', 'large'); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required