tags:

views:

58

answers:

3

I have pages like

index.php?key=toplist&list=magic

So if om on that page for example, i want the Magic option to be marked as selected in the select menu

<select name="skill" onchange="window.location.href=this.form.skill.options[this.form.skill.selectedIndex].value">
<option value="index.php?<?=QUERY_STRING?>&list=experience">Experience&nbsp;</option>
<option value="index.php?<?=QUERY_STRING?>&list=magic">Magic</option>
<option value="index.php?<?=QUERY_STRING?>&list=shielding">Shielding</option>
<option value="index.php?<?=QUERY_STRING?>&list=distance">Distance</option>
<option value="index.php?<?=QUERY_STRING?>&list=fishing">Fishing</option>
</select>

Thanks

+1  A: 

Use the selected attribute. In HTML, this would be:

<option value="x" selected>Label</option>

And in XHTML, it would be:

<option value="x" selected="selected">Label</option>

This is an HTML question, not a PHP-question, by the way.

kander
+3  A: 

For each <option> tag, you have to test if the value corresponds to the one that is to be considered as selected, and, for the one that is, you have to add the selected attribute :

<option value="..." selected="selected">blah blah</option>
Pascal MARTIN
+3  A: 

You add the selected attribute to the option tag. I typically do it with something like this:

$lists = array('experience', 'magic', 'shielding', 'distance', 'fishing');
foreach($lists as $list)
    echo "<option value=\"index.php?$QUERY_STRING&list=$list\"" . ($list == $_GET['list'] ? " selected" : "") . ">" . ucfirst($list) . "</option>"
Michael Mrozek