JustinPGriffen
JustinPGriffen

Reputation: 327

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.

Upvotes: 12

Views: 55726

Answers (6)

Bailey Parker
Bailey Parker

Reputation: 15903

Use selected on the <option> tag that names the user's current country:

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

So in PHP:

<select>
<?php
$countries = array('USA', 'Australia', 'Germany');
$current_country = 'Australia';

foreach($countries as $country) {
    if($country == $current_country) {
        echo '<option selected="selected">'.$country.'</option>';
    } else {
        echo '<option>'.$country.'</option>';
    }
}
?>
</select>

Upvotes: 5

Carlos Chaveztoro
Carlos Chaveztoro

Reputation: 1

A bit shorter:

foreach($paises as $key=>$value)
{
    if($key == "PE"){$activo = ' selected="selected" '; }else{$activo = "";}    
    echo "<option $activo value=\"$key\">$value</option>";
}

Upvotes: 0

Tyilo
Tyilo

Reputation: 30142

Use the selected attribute of the option tag.

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

Upvotes: 4

Francis Avila
Francis Avila

Reputation: 31641

The html looks like:

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

In the php loop that builds this html, compare the current option name with the user's current country (retrieved from a database or where-ever). When the two match, add selected

Upvotes: 1

Bj&#246;rn Kaiser
Bj&#246;rn Kaiser

Reputation: 9912

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.

Upvotes: 17

Oded
Oded

Reputation: 499212

You need to add a selected attribute to the option you want to select, as described in the spec.

<select id="viewSelector" name="when"">
    <option value="USA">USA</option>
    <option value="Australia" selected="selected">Australia</option>
    <option value="Germany">Germany</option>
</select>

In your script, you need to emit this attribute for whatever default you want to display.

Upvotes: 4

Related Questions