Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display selection from dropdown list? 1

Status
Not open for further replies.

JustKIDn

MIS
May 6, 2002
386
US
When I select an item from a dropdown box and submit.
It works correctly, but then when the list is displayed again, it doesn't show what was selected, but rather, the first item in the list.

So i.e. if "Select Item" is the first thing on the list. That's what you always see.

Another example; if the list was a list of states and someone selected TX, you would want to see TX next time the list box is displayed. Not "Choose State".

Anyone know how to fix this? hopefully without javascript. This is afterall, the PHP forum.

____________________________
JustKIDn

____________________________
Families can be together forever...
 
I'm assuming you're building the option list in your PHP code.
When you're building it, just put the word "selected" in the "<option>" tag with the same value as the one that was selected.
Code:
$sample = array('a','b','c','d');

for ($i=0;$i<count($sample);$i++) {
   echo '<option name=insamp value="' . $sample[$i] .'"';
   if ($_POST['insamp'] == $sample[$i]) echo ' selected';
   echo '>' . $sample[$i] . '<option>',"\n"; }
 
kenrbnsn,

Thanks for that bit of code. That was what I needed.

However I had to add;

selected [red]= $i [/red]

And to get it to work correctly, I also added;
[red]
}else{
echo '>'.$sample[$i].'</option>';
}
[/red]

I had played with the selected attribute earlier, but I just couldn't figure out the correct syntax without an example.

Thanks a lot, I think that's worth a star!

____________________________
JustKIDn

____________________________
Families can be together forever...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top