Page 496 - Beginning PHP 5.3
P. 496
Part III: Using PHP in Practice
By default, HTML_QuickForm can work with 23 different element types. All elements derive from the
HTML_QuickForm_element class. Following is a list of the most common element types, along with code
showing how to add the elements to your form.
You can find a complete list of HTML_QuickForm element types at http://pear.php.net/
manual/en/package.html.html-quickform.intro-elements.php .
Element Type Code to Add the Element to the Form
button $form - > addElement( “ button ” , “ field name ” , “ field value ” , $attrs );
checkbox $form - > addElement( “ checkbox ” , “ field name ” , “ field label ” , “ text
to display after checkbox ” , $attrs );
file $form - > addElement( “ file ” , “ field name ” , “ field label ” , $attrs );
hidden $form - > addElement( “ hidden ” , “ field name ” , “ field value ” , $attrs );
image $form - > addElement( “ image ” , “ field name ” , “ image URL ” , $attrs );
password $form - > addElement( “ password ” , “ field name ” , “ field label ” ,
$attrs );
radio $form - > addElement( “ radio ” , “ field name ” , “ field label ” , “ text
to display after button ” , “ field value ” , $attrs );
reset $form - > addElement( “ reset ” , “ field name ” , “ field value ” , $attrs );
select $form - > addElement( “ select ” , “ field name ” , “ field label ” , array(
“ option1Value ” = > “ option1Label ” , “ option2Value ” = >
“ option2Label ” , ... ), $attrs );
submit $form - > addElement( “ submit ” , “ field name ” , “ field value ” , $attrs );
text $form - > addElement( “ text ” , “ field name ” , “ field label ” , $attrs );
textarea $form - > addElement( “ textarea ” , “ field name ” , “ field label ” ,
$attrs );
The optional $attrs argument is a list of any attributes to add to the element tag. It can be in the form of
an associative array, or a simple string (such as ‘ name = “ value “ ).
‘
You can see that some elements are created with a value (such as buttons, where the value is the button
label), whereas other elements are created with a field label (displayed to the left of the field by default).
You can always set your own value or label for a field by calling the element object ’ s setValue() or
setLabel() method after you ’ ve created the object — for example:
$textArea = $form- > addElement( “textarea”, “field name”, “field label”,
$attrs );
$textArea- > setValue( “Default text” );
Here ’ s a simple example script that uses HTML_QuickForm to create a login form (without any validation
or filtering):
< !DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd” >
< html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en” >
458
9/21/09 9:14:53 AM
c15.indd 458 9/21/09 9:14:53 AM
c15.indd 458