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:');
$form->addElement($element);
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
$form->addElement($element);
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');
$element->setSize(40);
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. |
Next: Text, Password, Textarea >>
More Graphic Design Articles
More By Apress Publishing