Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I mark current option as selected?

Tags:

html

php

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

like image 580
Charlie Avatar asked Dec 23 '22 01:12

Charlie


1 Answers

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>"
like image 63
Michael Mrozek Avatar answered Jan 07 '23 04:01

Michael Mrozek