Skip to content
Advertisement

How do I set the selected item in a drop down box

Is there any way to set the selected item in a drop down box using the following ‘type’ code?

<select selected="<?php print($row[month]); ?>"><option value="Janurary">January</option><option value="February">February</option><option value="March">March</option><option value="April">April</option></select>

The database holds a month.. and I want to allow on the edit page, them to choose this month.. but it to be pre-filled with their current setting?

Advertisement

Answer

You need to set the selected attribute of the correct option tag:

<option value="January" selected="selected">January</option>

Your PHP would look something like this:

<option value="January"<?=$row['month'] == 'January' ? ' selected="selected"' : '';?>>January</option>

I usually find it neater to create an array of values and loop through that to create a dropdown.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement