RE: [PHP] Trying to keep a dropdown selection sticky
| From: | Warren Vail | Date: | Sun, 06 Jul 2008 04:27:08 +0000 |
| Subject: | RE: [PHP] Trying to keep a dropdown selection sticky | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-276324@lists.php.net to get a copy of this message | ||
The selection will stick until you do a form submit. The most common
practice in PHP is to update the database with the value from the form or at
least save it in a session. After the update you can redisplay your form
after fetching the DB contents or getting the selection from the session
into a variable (say $choice) and modify the loop below in your form so that
the echo inside the loop is broken into the following;
foreach ($state_list as $key => $value) {
echo "<option value=\"$key\"";
if($key == $choice) echo " selected"; // now it's sticky
;-)
echo "> $value</option>\n";
}
When you fail to indicate which item in a select list is "selected" most
browsers will default to showing the first entry (effectively unsticking
your choice).
Good luck,
Warren Vail
> -----Original Message-----
> From: Michael S. Dunsavage [mailto:mikesd@ptd.net]
> Sent: Friday, July 04, 2008 6:32 PM
> To: php-general@lists.php.net
> Subject: [PHP] Trying to keep a dropdown selection sticky
>
> I have a form I want to keep sticky, but I can't figure out
> how. I got all the <INPUT>'s to be sticky....
>
> The select script
>
>
> <?php
> echo '<strong>State</strong><br>';
> echo '<select name="State">';
> foreach ($state_list as $key => $value) {
> echo "<option value=\"$key\"> $value</option>\n";
> }
> echo '</select>';
> echo '<br>';
>
>
>
> ?>
>
>
> so now how do I keep that sticky?
>
> --
> Michael S. Dunsavage
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>