Need drop down menu to have existing value as default

Codes here !

Moderators: egami, macek, gesf

Post Reply
php-forum Fan User
php-forum Fan User
Posts: 973
Joined: Mon Oct 01, 2012 12:32 pm

Tue Apr 22, 2014 12:47 am

This is accomplished when you're building the select box. The code looks a little different depending on whether you're doing object based approach or procedural, but basically, when you're building your select box, if you have a stored value for that field, its id should match the value of the same selection in the select box. So populating the select box would look something like this:

Code: Select all

echo '<select name="whatever"><option>Pick something already</option>';
While (true){ 

// just using true for example, but you'd want to use a 
// real condition that will be false after the last item 
// from your query has been handled

$selected = ($row['id'] == $current_value)?' selected="selected" ':'';
echo '<option value=$row['id']' . $selected . '/>' . $row['description'] . '</option>';
echo '</select>';

Post Reply