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>';

Change this

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

to

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

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

Select options with Pressing Ctrl-button

OR

Select options by click and drag the pointer

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??

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>';
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.