Forum Moderators: coopster

Message Too Old, No Replies

Dynamic Select

How to set the selected-index based on database

         

webfoo

8:36 pm on Jun 11, 2008 (gmt 0)

10+ Year Member



I have a PHP-powered HTML form that updates a MySQL database. The database has a field containing one of three different values. The form has a dropdown box offering these three values. Using PHP/SQL, how can I set the dropdown box to automatically select the current value of the database field?

eelixduppy

1:06 am on Jun 12, 2008 (gmt 0)



Here is something I wrote up some time ago. This also implements AJAX for a more user-friendly interface, but you can also simply remove that. Take a look: [webmasterworld.com...]

webfoo

12:52 am on Jun 14, 2008 (gmt 0)

10+ Year Member



Thanks, but I can't understand your script, eelix. All I want to know is how to automatically select the right value from a dropdown. I think it has something to do with a selectedindex ?

eelixduppy

1:48 pm on Jun 15, 2008 (gmt 0)



I'm actually sorry. I misread your original question; I must have been going to quickly or something. Anyway, you must compare the value of the select with what's in the database and then echo accordingly. So it would look something like this:

<option value="one" <?php echo ($row['col'] === 'one') ? 'selected': ''; ?> />
<option value="two" <?php echo ($row['col'] === 'two') ? 'selected': ''; ?> />
<option value="three" <?php echo ($row['col'] === 'three') ? 'selected': ''; ?> />

etc...

Hope this helps. Sorry again.