Show selected value in a drop down menu

I have a multisite setup and I am adding some additional information fields in the profile edit screen in my theme’s dashboard. I am primarily using text fields however I have a drop down selection menu for the author’s country

<select name="country" class="mid2" id="country"  value="<?php echo $userdata->country ?>" style="width:150px;">

My problem is that if a user selects “Togo” for example it registers the change (I know this because it echoes it in the author’s bio page) but the default value in the drop down menu (In this case the United States) always stays the same.

Read More

Any ideas on how I can have it display whichever country the author has selected?

Related posts

Leave a Reply

2 comments

  1. WordPress has a great little function built in for handling selections.

    <option <?php selected('value1', 'value2');?> value='foo'>Bar</option>
    

    You can also check out these form handling functions:

    Your HTML syntax is wrong as well. Per W3C, ‘value’ is not an attribute of the select tag. The value of a select is defined in the options of a select. It should look like this:

    <select name="country" class="mid2" id="country" style="width:150px;">
        <option value="US" <?php selected('US', $userdata->country);?>>USA</option>
        <option value="ES" <?php selected('ES', $userdata->country);?>>Spain</option>
        <option value="FR" <?php selected('FR', $userdata->country);?>>France</option>
    </select>
    
  2. So you say it registers the change, so it doesn’t really sound like its a WordPress problem but here is how you would display that anyways.

    <option value="your_value" <?php if(this is the Country you selected)echo 'selected="selected"'; ?> >The Country</option>