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 - Hidden, Select (Page 5 of 13 )
The hidden element produces an HTML <input type="hidden"> tag. The browser doesn’t display any input widget when it sees this tag. Hidden elements are useful for passing values from page to page in a form. Remember, though, that a user can see the value of a hidden element by viewing the source code to a Web page. They are displayed without a label or other formatting.
These are the valid arguments when creating a hidden element:
$elementName: The name attribute of the element’s <input> tag.
$elementValue: The default value to use for the element. This is overridden by a submitted form value.
$attributes: Arbitrary element attributes, as a string or an associative array.
The select element produces the drop-down box displayed by the HTML <select> tag.
These are the valid arguments when creating a select element:
$elementName: The name attribute of the element’s <input> tag
$elementLabel: The label of the element in the form
$options: An array holding choices to display in the menu
$attributes: Arbitrary element attributes, as a string or an associative array
The following are its methods:
setMultiple(): This sets the multiple attribute, which controls whether the user can select more than one element from the menu.
getMultiple(): This gets the value of the multiple attribute.
setSelected(): This sets an array containing the selected value or values from the menu.
getSelected(): This gets the array of selected values from the menu.
setSize(): This sets the size attribute, which controls how many options to display at once if multiple is set.
getSize(): This gets the value of the size attribute.
getPrivateName(): This returns the element name as displayed in the form. When multiple is set, this is the result of getName() with [] appended to it.
addOption($text, $value, $attributes): This adds an option to the end of the element’s menu. The $attributes argument is optional.
loadArray($arr, $values): This adds the options in the associative array $arr to the end of the element’s menu. The values of the new options are taken from the keys of $arr. The text of the new options is taken from the values of $arr. The optional $values argument holds an array or comma-separated string of values to set as selected in the menu. For example:
loadDbResult($result, $textCol, $valueCol, $values): This adds options from the PEAR DB DB_Result object to the end of the element’s menu. The $textCol argument holds the name of the column in the DB_Result object to use as the text of each option, and the $valueCol argument holds the name of the column to use as the value of each option. Both $textCol and $valueCol are optional. If they are omitted (or NULL), then the text of each option is the first column in the DB_Result object, and the value of each option is the second column. As in loadArray(), the optional $values argument holds an array or comma-separated string of values to set as selected in the menu.
loadQuery($conn, $sql, $textCol, $valueCol, $values): This runs a database query and adds options from the query results to the end of the element’s menu. The $conn argument is either a valid PEAR DB database connection handle or a string containing a PEAR DB Data Source Name (DSN). If $conn is a connection handle, then the SQL query in $sql is sent to the database using that connection handle. If $conn is a DSN, then a connection is established using the DSN, and the query in $sql is sent to the database using that new connection handle. The optional $textCol, $valueCol, and $values arguments behave as they do in the loadDbResult() method. Because loadQuery() accepts its first argument by reference, you can’t pass a DSN as a string literal but must instead pass a variable that holds the DSN. For example:
// The right way to do it $dsn = 'mysql://user:password@host/db'; $s->loadQuery($dsn,'SELECT flavor,id FROM ice_cream'); // The wrong way to do it: $s->loadQuery('mysql://user:password@host/db','SELECT flavor,id FROM ice_cream');
If you pass $options as indexed array, the values in the array appear as choices, and the value attribute of each <option> tag is set to the numeric key of each array element. For example:
The first element of the $subjects array has the key 0 and the value Math. So the first <option> tag displayed has its value attribute set to 0, and the text between the <option> and </option> tags is Math. If you select Math from the list of choices and submit the form, the value of the submitted form variable subject is 0. If you select Anatomy from the list of choices and submit the form, the value of subject is 1.
To specify <option> values explicitly, define both keys and values in the $options array:
Note that the order in which the choices appear in the drop-down box is the same as the order in which they are defined in the array. To have string <option> values, use string keys in the array:
Just as before, the <option> value attributes are set to the keys of the $elevator array. Because those keys are strings in this example, the value attributes are strings as well:
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.