0

Hi, just started to learn php so fairly new to the concepts.
Need some help making a sticky select form from an array.
This is what i have so far:

print '<label>Select your favourite colours:';
            print '</label>';
                        print '<select name="colour" multiple="multiple">';
                                $colours= array('R'=>'Red','O'=>'Orange','Y'=>'Yellow','G'=>'Green','B'=>'Blue','P'=>'Purple','Pi'=>'Pink');



foreach($colours as $colour => $cname){
if($csel === $colour){
$sel = 'selected="selected"';
}
else{
$sel = '';
}
print '<option value="'.$colour.''.$sel.'>'.$cname.'</option>';
}
print '</select>';
2
Contributors
5
Replies
7
Views
4 Years
Discussion Span
Last Post by Bachu
0

Change this

print '<option value="'.$colour.''.$sel.'>'.$cname.'</option>';

to

print '<option value="'.$colour.'" '.$sel.'>'.$cname.'</option>';
0

Thankyou vmuch! That worked, but if choosing multiple values it only highlights the last selected, not all that were selected??

0

Select options with Pressing Ctrl-button

OR

Select options by click and drag the pointer

0

Yes, when i select more than one option, (i.e: yellow and orange) and submit the form, it only highlights the last selection - orange and not the yellow.
In the address bar it shows both i.e( filename.php?colour=O&colour=Y) but only shows one value being sticky??

0

Use post method for form submission.
In your form, set

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

In your action page, set

$csel = array();
$csel = $_POST['colour'];
foreach($colours as $colour => $cname){
$sel = '';
foreach ($csel as $clr) {
    if($clr == $colour){
        $sel = 'selected="selected"';
    }
}
print '<option value="'.$colour.'" '.$sel.'>'.$cname.'</option>';
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.