Jump to content

select correct item from dropdown list


centenial

Recommended Posts

Hi,

I have a dropdown list similiar to this:

[code]<select name="dropdown">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>[/code]

Now, in my database table I have a field called 'dropdown' and in it is one of those values (Let's say "3" for an example). Now, on my edit page I want to display that dropdown (select) list but have the correct item selected. Like this (For example):

[code]<select name="dropdown">
<option value="1">1</option>
<option value="2">2</option>
<option value="3" selected>3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>[/code]

Now if I change it to "2", "5", or any other option next time I go to the edit page the correct item should be selected. How should I go about doing this? Is there an easy function that accomplishes it?

Thanks!
Link to comment
https://forums.phpfreaks.com/topic/10544-select-correct-item-from-dropdown-list/
Share on other sites

The easiest way is to add the value as the first options like this (if three is selected)
[code]
<select name="dropdown">
<option value="3">3</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.