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.)
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: