populating drop down menus

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kdehl113
Forum Newbie
Posts: 2
Joined: Sat Jan 10, 2004 1:37 pm

populating drop down menus

Post by kdehl113 »

Hello, I have been searching for a while for this question. I have a form which has a couple of drop down menus on it. The form is written in html and is passed to a php script. I want the php script to validate the form and to redisplay info already enetered by the user, only if it fails validation. So what I need to know is how do I get the drop downs to display the data in it's variable back to the drop down?

Kevin
basdog22
Forum Contributor
Posts: 158
Joined: Sun Nov 30, 2003 3:03 pm
Location: Greece

Post by basdog22 »

the name or id of the form elements you use are passed as variables to php. So if your drop down is like:

<select name="choice">
<option>var1</option>
<option>var2</option>
<option>var3</option>
</select>

then the variable passed is $choice (will hold the selected: var1, var2 or var3)

so you can do:

Code: Select all

<?php
echo "<select name="choice">
<option selected="selected">$choice</option>
<option>var2</option>
<option>var3</option>
</select>
";

?>
kdehl113
Forum Newbie
Posts: 2
Joined: Sat Jan 10, 2004 1:37 pm

Drop Down

Post by kdehl113 »

Thanks, worked great.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

You could also take a loot to this code snippet: viewtopic.php?t=14029
Post Reply