11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 13• WORKING WITH HTML FORMSwww.it-ebooks.info$language = $fieldSet->addSelect('language', null, array('options' => $languages));$language->setLabel('Choose Your Favorite Programming Language:');$language->addRule('required', 'Please choose a programming language.');$fieldSet->addElement('submit', null, 'Submit!');if ($form->validate()) {echo "SUCCESS";}$renderer = HTML_QuickForm2_Renderer::factory('default')->setOption(array('group_errors' => true));echo $form->render($renderer);?>Let’s review Listing 13-1’s key features:• The $languages array defines the array that populates the languages select boxshown in Figure 13-2. This is an associative array because we need to define boththe option key and value used to populate the select box.• The HTML_QuickForm2 constructor accepts several parameters, including theform’s ID attribute (languages) and the method (POST).• The addFieldSet() method creates a new field set container.• The addText() method adds a new input field. Notice how we are associating thiswith the field set rather than the form, which indicates to HTML_QuickForm2 thatthe input field should be placed inside the field set. The addRule() method givesyou the ability to validate this field, in this example determining whether the fieldis required. The required flag is only one of several available to you; you canvalidate a field’s length, compare it with ranges and other values, and much more.Consult the HTML_QuickForm2 documentation for more details.• The addSelect() method creates a select box. Notice how the $languages array ispassed into the method.• The validate() method validates the form based on the defined rules. Of course,in a real-world situation you’ll want to provide the user with a more effectivestatus message. Notice how this appears before the form is rendered. Neglectingto validate the form before it is rendered will cause the form to not be validatedproperly.• Finally, the form is rendered using the render() method. In this example, we’reusing HTML_QuickForm2’s default rendering mechanism; if you have moreflexible formatting needs, look into HTMLQuickForm2’s powerful renderingoptions.300

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!