Home arrow Graphic Design arrow Page 3 - Using HTML Quickform for Form Processing

Using HTML Quickform for Form Processing

With HTML_QuickForm version 3.1, you can have a consistent look across all your forms and a simplified file upload. This chapter starts with the basics then shows you how to process submitted form data with HTML_QuickForm. (From the book, Essential PHP Tools: Modules, Extensions, and Accelerators, by David Sklar, Apress, 2004, ISBN: 159059280.)

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 193
September 01, 2004
  1. · Using HTML Quickform for Form Processing
  2. · Steps for the Example
  3. · Individual Elements
  4. · Text, Password, Textarea
  5. · Hidden, Select
  6. · Checkbox, Radio
  7. · Submit, Reset, Button, Image
  8. · File, advcheckbox, Static
  9. · Header, Link, HTML
  10. · Element Groups
  11. · Processing Submitted Data
  12. · Without a Callback Function
  13. · Setting Validation Rules

print this article

Using HTML Quickform for Form Processing - Individual Elements
(Page 3 of 13 )

There are two ways to add elements to a form. The first is to use the addElement() method of the form object. This is how you built the form in the previous section. Because you can pass addElement() arguments that control various aspects and attributes of the form element, itís a flexible one-step way to populate your form. The other way to add elements to a form is to create elements as separate objects with the createElement() static method. This method takes the same arguments as addElement() but returns an object. You can then add the element to the form by passing the object to addElement(). In this example, two text elements are added to the form: the first using just addElement() and the second using createElement() with addElement().

// $form is an HTML_QuickForm object
// add the text field in one step with addElement()
$form->addElement('text','first_name','First Name:');
// create the element with createElement() and then pass it to addElement()
$element =& HTML_QuickForm::createElement('text','last_name','Last Name:');

To avoid making copies of the object as it is created, it is necessary to retrieve the result of createElement() by reference with the & operator. This is not necessary in PHP 5. Using createElement() adds an extra step, but it also gives you access to the element object. You can call methods on the element object to alter its behavior. For example, the text element has methods called setSize() and setMaxlength(), which set the size and maxlength attributes of the element, respectively:

$element =& HTML_QuickForm::createElement('text','last_name','Last Name:');
$element->setSize(10); // Render the element as ten characters wide $element->setMaxlength(30); // Allow no more than 30 characters of input

If you add elements to the form without using createElement(), you can retrieve element objects later with getElement(). Pass it the name of an element, and it returns the corresponding element object. You must retrieve the object by reference so that any changes you make to it are accurately reflected in the form:

$form->addElement('text','first_name','First Name:');
$element=& $form->getElement('first_name');

Some element methods retrieve information about the element instead of modifying it. For example, getName() returns an elementís name:

$form->addElement('text','first_name','First Name:');
$element=& $form->getElement('first_name');
// this sets $name to "first_name"
$name = $element->getName();

All elements have the methods setName(), getName(), setValue(), getValue(), setLabel(), and getLabel().

This chapter is from Essential PHP Tools: Modules, Extensions, and Accelerators, by David Sklar, (Apress, 2004, ISBN: 1590592808). Check it out at your favorite bookstore today.

Buy this book now.

blog comments powered by Disqus

- Customizing Wordpress Favicons and Gravatars
- Building Corner Effects with Transparent Bac...
- 3D Graphics Technology: VRML Part I - Introd...
- Creating Visual Effects
- Web Page Design Overview
- Creating Artistic Photographs
- Working with Tools in Paint Shop Pro 8
- Using HTML Quickform for Form Processing
- Introduction to Adobe FrameMaker
- WebLogic Workshop, WebLogic Platform, and th...
- Planning the Site
- Working with Web Services
- WebLogic Workshop Application Development Ba...
- Scanning Images for Web Use
- Web Graphics Overview

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials