PHP, databases and dropdown lists

I have a page that works OK at the moment but am looking to change an input box to a dropdown list. The page picks up a record and then sticks the info into boxes for the user to edit then save. The issue I’m having is getting the dropdown box to default to the value retrieved from the database. The important bit of code in my php file is:

echo '
<tr><td><label for="status">Status: </label></td>

<td><select name="status" id="status" class="narrow">

<option  value="Not started">Not started</option>

<option  value="In progress">In progress</option>

<option value="Update done">Update done</option>

<option value="Book only">Book only</option>


I have the variable $status which comes from the database but cannot get the selected option to show correctly. How do I get it to work? Thanks.

Well, you need to add a SELECTED parm to the one you want to be active…

<option value="In progress" selected>In progress</option>

To do that from a database, you need to just add it when you hit the selected option.
Just compare the selected one to the one in value and if the same display the “selected” parameter.

Thanks. In the meantime rewrote the page to get rid of so much of the echo which has allowed we to do it quite easily. Sorted now.

Sponsor our Newsletter | Privacy Policy | Terms of Service