print a select menu : Form « Form « PHP






print a select menu

 
function input_select($element_name, $selected, $options, $multiple = false) {
    print '<select name="' . $element_name;
    if ($multiple) { print '[]" multiple="multiple'; }
    print '">';

    $selected_options = array();
    if ($multiple) {
        foreach ($selected[$element_name] as $val) {
            $selected_options[$val] = true;
        }
    } else {
        $selected_options[ $selected[$element_name] ] = true;
    }

    foreach ($options as $option => $label) {
        print '<option value="' . htmlentities($option) . '"';
        if ($selected_options[$option]) {
            print ' selected="selected"';
        }
        print '>' . htmlentities($label) . '</option>';
    }
    print '</select>';
}
  
  








Related examples in the same category

1.print a radio button or checkbox
2.print a submit button
3.print a text box
4.print a textarea
5.HTML elements for use in forms
6.HTML Form Elements
7.HTML form for submitting data
8.Working with Multipage Forms
9.Process the results