Re: Retaining listbox selections
| From: | Larry Martell | Date: | Sat, 21 Dec 2013 18:49:07 +0000 |
| Subject: | Re: Retaining listbox selections | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-322592@lists.php.net to get a copy of this message | ||
On Fri, Dec 20, 2013 at 6:08 PM, Ashley Sheridan
<ash@ashleysheridan.co.uk> wrote:
> On Fri, 2013-12-20 at 18:03 -0500, Larry Martell wrote:
>> I have a listbox and when the user selects items they are highlighted. Then
>> I process the form and when I generate the next web page (which has the
>> listbox) I want the selected items still to be highlighted. Can I do this
>> from PHP when I generate the listbox or must that be done client side from
>> javascript?
>
> Yes, it's pretty easy, here's an example:
>
> <select name="colour">
> <?php
> $colours = array('red','blue','green','yellow');
>
> foreach($colours as $colour)
> {
> $selected = (isset($_POST['colour']) && $colour ==
> $_POST['colour'])?'selected="selected"':'';
>
> echo "<option value=\"$colour\"
> $selected>$colour</option>";
> }
> ?>
> </select>
Thanks!