Discussion:
Retaining listbox selections
Larry Martell
2013-12-20 23:03:06 UTC
Permalink
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?
Stuart Dallas
2013-12-20 23:06:06 UTC
Permalink
Post by Larry Martell
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?
This is pretty basic HTML. Simply add the attribute selected="selected" to the option tag you want to be selected. You could do it in Javascript, but since you already have PHP generating the form you might as well do it there.

-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Larry Martell
2013-12-21 18:48:46 UTC
Permalink
Post by Stuart Dallas
Post by Larry Martell
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?
This is pretty basic HTML. Simply add the attribute selected="selected" to the option tag you want to be selected. You could do it in Javascript, but since you already have PHP generating the form you might as well do it there.
Thanks.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ashley Sheridan
2013-12-20 23:08:19 UTC
Permalink
Post by Larry Martell
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,
Ash
http://www.ashleysheridan.co.uk
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Larry Martell
2013-12-21 18:49:07 UTC
Permalink
On Fri, Dec 20, 2013 at 6:08 PM, Ashley Sheridan
Post by Ashley Sheridan
Post by Larry Martell
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?
<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!
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Continue reading on narkive:
Loading...