Forum Moderators: open

Message Too Old, No Replies

Pull down menu initial value

easy question!

         

wonderboy

2:07 pm on Jul 26, 2004 (gmt 0)

10+ Year Member



Hi,

If I have a pulldown menu with:

A
B
C
D

And I select "C" and then submit to a second page with the same pulldown menu, how do I make sure that C is the initially selected option, without taking it out of order?

Thanks,

W.

BlobFisk

3:33 pm on Jul 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<option ... >A</option>
<option ... >B</option>
<option value="c" selected="selected">C</option>
<option ... >D</option>

C will be the visibile (selected) option regardless of where it appears in the list.

HTH

wonderboy

5:42 pm on Jul 26, 2004 (gmt 0)

10+ Year Member



I meant whichever item was selected on the first menu would be selected automatically on the second. Sorry for the wording of the question...

It is for a user profile update module, so if they want to update their information, the information they have already inputted to the database will be in the form, they just have to edit the bits they need.
I have done the normal text boxes, but I can't seem to find a solution for the pull down menus!

Any more ideas? and thanks for replying.

W.

Birdman

6:09 pm on Jul 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



what you need to do is create an array of the dropdown values, then loop through them. In each iteration of the loop, you compare it to the user's current setting for that field.

I don't know what server language you are using, so I'll use PHP in the example.

EXAMPLE

$user_stat = "C"; //the user's current setting
$dropdown = "<select name=\"something\">";

$values = array("A", "B", "C", "D");

foreach ($values as $val) {
$selected = ($user_stat == $key)? " selected=\"\"selected" : "";
$dropdown .= "<option value=\"".$val."\".$selected.">".$val."</option>";
}

$dropdown .= "</select>";

print $dropdown;

ergophobe

8:51 pm on Jul 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




$selected = ($user_stat == $key)? " selected=\"\"selected" : "";

Small typo with the quotes there. Since I know dkin is using PHP and struggling a fair bit, I figured I would correct:

$selected = ($user_stat == $key)? " selected=\"selected\"" : "";

Tom

wonderboy

10:29 pm on Jul 26, 2004 (gmt 0)

10+ Year Member



Cheers guys,

Will have a mess around with it ASAP!

W.

wonderboy

12:47 pm on Jul 27, 2004 (gmt 0)

10+ Year Member



I am using PHP, and the example provided just creates the dropdown list, but doen't select "C"

What is $key in the example?

Cheers,

W.

Birdman

2:11 pm on Jul 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello again,

$key should really be $val. If you change that, it should work. Sorry 'bout that!

wonderboy

6:47 pm on Jul 27, 2004 (gmt 0)

10+ Year Member



Legend!

Thanks, works a treat.

W.