Change Selected Option In Select (form)
I have an intial group of options like these:
Solution 1:
One way:
<selectname="item[type]"><option<?=$test==0?'selected="selected"':'';?>value="0"class="dr">First</option><option<?=$test==1?'selected="selected"':'';?>value="1"class="dr">Second</option><option<?=$test==2?'selected="selected"':'';?>value="2"class="dr">Third</option><option<?=$test==3?'selected="selected"':'';?>value="3"class="dr">Fourth</option></select>
Another:
<?$selected[$test] = 'selected="selected"'; ?><selectname="item[type]"><option<?=$selected[0];?>value="0"class="dr">First</option><option<?=$selected[1];?>value="1"class="dr">Second</option><option<?=$selected[2];?>value="2"class="dr">Third</option><option<?=$selected[3];?>value="3"class="dr">Fourth</option></select>
Solution 2:
<selectname="item[type]"id="selectBoxId"><optionvalue="0"class="dr">First</option><optionvalue="1"class="dr">Second</option><optionvalue="2"class="dr">Third</option><optionvalue="3"class="dr">Fourth</option></select><scripttype="text/javascript">var test = "<?= $test; ?>";
if (test != '' && parseInt(test)) {
document.getElementById('selectBoxId').selectedIndex = test;
}
</script>
Solution 3:
Remove "[type]" from select name, make it simple to "item". Then execute this code.
$test = isset($_POST['item']) ? $_POST['item'] : "0";
Solution 4:
// assuming you are using a loop: in the loop where you create the options$selected_html = $test == $loop_var ? ' selected="selected" ' : '';
echo"<option value=\"$loop_var\" class=\"dr\"$selected_html>$text</option>";
Post a Comment for "Change Selected Option In Select (form)"