Re: Form display and processing module?
| From: | Rasmus Lerdorf | Date: | Fri, 07 May 2004 15:36:18 +0000 |
| Subject: | Re: Form display and processing module? | ||
| References: | 1 2 3 4 | Groups: | php.pecl.dev |
| Request: | Send a blank email to pecl-dev+get-624@lists.php.net to get a copy of this message | ||
On Fri, 7 May 2004, David Sklar wrote:
> There are some areas for improvement here, but input validation is not
> my main concern. There are many cumbersome steps required for, say,
> generating a <select> menu with appropriate defaults. Here's a function
> that handles single or multiple valued select menus:
>
>
> function input_select($field_name, $values, $options, $multiple = false) {
> // print out the <select> tag
> print '<select name="' . $field_name;
> // if multiple choices are permitted, add the multiple attribute
> // and add a [] to the end of the tag name
> if ($multiple) { print '[]" multiple="multiple'; }
> print '">';
>
> // set up the list of things to be selected
> $selected_options = array();
> if ($multiple) {
> foreach ($values[$field_name] as $val) {
> $selected_options[$val] = true;
> }
> } else {
> $selected_options[$values[$field_name]] = true;
> }
>
> // print out the <option> tags
> foreach ($options as $option => $label) {
> print '<option value="' . htmlentities($option) .
> '"';
> if (array_key_exists($option, $selected_options)) {
> print ' selected="selected"';
> }
> print '>' . htmlentities($label) . '</option>';
> }
> print '</select>';
> }
>
> To redisplay the menu after form submission with any selections
> preserved, you'd call it like this:
>
> $colors = array('red' => 'Red', 'green' =>
> 'Green', 'blue' => 'Blue');
> input_select('color', $_POST, $colors, true);
>
> This kind of form widget display is, I think, a common operation, and
> one that takes way too much repeated code each time some random user
> needs to do it. So it's these kinds of things I'd like to see simplified.
Right, and this fundamentally belongs in PEAR. Get it into the default
PEAR classes that ship with PHP. Putting this into the core makes no
sense at all.
-Rasmus