Adding "selected" into option from matching variable. PHP

Posted by Dolan on Stack Overflow See other posts from Stack Overflow or by Dolan
Published on 2012-06-27T03:05:32Z Indexed on 2012/06/27 3:16 UTC
Read the original article Hit count: 231

Filed under:
|
|

I have $country which stores the 2 letter country code of a user. US for example.

<select id="countries">                     
    <option value="US">United States</option>
    <option value="EN">England</option>
    <option value="AU">Australia</option>
    <option value="CA">Canada</option>
    etc.
</select>

What's the best practice to add "selected" to the option of the users country code?

I'm against adding a million if's. Also note, the countries are harcoded in HTML, so no array. :(

© Stack Overflow or respective owner

Related posts about php

Related posts about option