Setting defaults in a multi-valued menu

 
<?
$main_dishes = array('cuke' => 'B',
                     'stomach' => "B",
                     'tripe' => 'C',
                     'taro' => 'S',
                     'giblets' => 'E', 
                     'abalone' => 'F');

print '<select name="main_dish[]" multiple="multiple">';

$selected_options = array();
foreach ($defaults['main_dish'] as $option) {
    $selected_options[$option] = true;
}

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

    print "\n";
}
print '</select>';
?>
  
  








Related examples in the same category

1.Form select input
2.An HTML Form Including a SELECT Element
3.A
4.A day choice
5.An HTML Form with a select Element
6.Validating a drop-down menu with in_array()
7.Displaying a
8.Generating a dynamic pull-down menu
9.Setting a default value in a
10.Creating Form Elements Based on the Current Time and/or Date
11.Creating Form Elements with Multiple Options
12.One choice for each day from 1 to 31
13.One choice for each year from last year to five years from now