PDA

View Full Version : change "SELECTED" dynamically



joeltonnberg
August 25th, 2007, 05:35 AM
Hi!

I want to set the initial value in a ordinary drop down menu dynamically, depending on the value from a recordset.

for example, if I have a menu as such:


<select>
<option>option 1</option>
<option>option 2</option>
<option>option 3</option>
</select>and want the selected option when page load to correspond with the following:


<?php echo $row_Recordset['option']; ?>how would I go about to do that?

tfoston
August 25th, 2007, 11:45 AM
Say you pulldown is called "MyPulldown" and you want to get to the second option for example you would use

instance.SelectedIndex = value

Where "instance" = MyPulldown (the name of the pulldown) and value is the
option number you want to get to so.
Mypulldown.selectedIndex = 2 would set the pulldown to the second item

icio
August 25th, 2007, 02:08 PM
But to do it in PHP is actually quite labourious. What you need to do is test that the value is the value for the option each time you write out the option.

Let's say you've got the value of the option you want to store in the variable `$option`:

<?php
$option = "1";
?>
<select name="choice">
<option value="1" <?=$option=="1"?' selected="selected"':''?>>Option 1</option>
<option value="2" <?=$option=="2"?' selected="selected"':''?>>Option 2</option>
<option value="3" <?=$option=="3"?' selected="selected"':''?>>Option 3</option>
</select>

Obviously if you're outputing the list of options in some kind of loop this is a bit neater. Hope that helps :thumb: