Darayus Nanavati Darayus Nanavati - 4 months ago 11
PHP Question

HTML define what selection option is active

I have a select box with the following (it has been shortened):

<select id="viewSelector" name="when" style="width:92%;">
<option value="USA">USA</option>
<option value="Australia">Australia</option>
<option value="Germany">Germany</option>
</select>


If the user logs into the control panel and wants to change his country, he gets presented with this form. My problem is that everytime USA is the default and I can not change this depending on the users country. For example, the user lives in Australia. He wants to change his country to USA and goes to this page. I want the country that is displayed to be Australia. Let me know if this makes sense.

Answer

To preselect a value, just add the selected attribute to the desired option.

<select id="viewSelector" name="when" style="width:92%;">
    <option value="USA">USA</option>
    <option value="Australia" selected="selected">Australia</option>
    <option value="Germany">Germany</option>
</select>

This will preselect Australia for example.