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 following is a sample usage:
$form->addElement('hidden','code',120374);
This is the sample HTML:
<input name="code" type="hidden" value="120374" />
select
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:
$s =& $form->createElement('select','animal','Animal: ');
$opts = array('dog' => 'woof', 'cat' => 'meow', 'cow' => 'moo');
$s->loadArray($opts,'cat');
$form->addElement($s);
The menu is displayed with the following HTML:
<tr>
<td align="right" valign="top"><b>Animal: </b></td>
<td valign="top" align="left"><select name="animal">
<option value="dog">woof</option>
<option value="cat" selected="selected">meow</option>
<option value="cow">moo</option>
</select></td>
</tr>
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:
$subjects = array('Math','Ice Fishing','Anatomy');
$form->addElement('select','subject','Subject(s): ',$subjects);
When this form is displayed, the select element is expressed with the following HTML:
<select name="subject">
<option value="0">Math</option>
<option value="1">Ice Fishing</option>
<option value="2">Anatomy</option>
</select>
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:
$elevator = array(22 => 'Rooftop Dining Room',
4 => 'Library',
1 => 'Lobby');
$form->addElement('select','which_floor','Your Floor: ',$elevator);
This produces the following HTML:
<select name="which_floor">
<option value="22">Rooftop Dining Room</option>
<option value="4">Library</option>
<option value="1">Lobby</option>
</select>
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:
$elevator = array('Rooftop Dining Room' => '22nd Floor',
'Library' => '4th Floor',
'Lobby' => '1st Floor');
$form->addElement('select','which_floor','Your Floor: ',$elevator);
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:
<select name="which_floor">
<option value="Rooftop Dining Room">22nd Floor</option>
<option value="Library">4th Floor</option>
<option value="Lobby">1st Floor</option>
</select>
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: Checkbox, Radio >>
More Graphic Design Articles
More By Apress Publishing