setting form selection

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
CraniumDesigns
Forum Newbie
Posts: 18
Joined: Fri Nov 07, 2003 1:35 am

setting form selection

Post by CraniumDesigns »

ok, say i have a page where a user can edit their setttings. say one of those things is their, oh i don't know, eye color. say their eye color si already stores as blue, but for some reason they actually meant to set it to green.

ok. if i know how to detect what color it is set as (blue), how do i have a drop down select box that has all the colors avilalable, but since it detects blue as the already set color, that is already selected?

i know in just html it is something like this:

Code: Select all

<select name="eye_color">
			<option value="Blue" selected>Blue</option>
			<option value="Green">Green</option>
                        <option value="Brown">Brown</option>
	    </select>
but how do i set the selected parameter to go to whatever it currently is set at in the DB?
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

There are plenty of ways of doing this. Personaly I use a function() which builds the form (and makes life a whole lot easier) but if you don't know too much about PHP something like the following will also work...

Code: Select all

<select name="eye_color">
<option value="Blue" <?php if($eyeColor=="Blue") echo "select="selected"" ?>>Blue</option>
<option value="Green" <?php if($eyeColor=="Green") echo "select="selected"" ?>>Green</option>
</select>
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Also, take a look to this Code Snippet I wrote: PHP/HTML: Print <select><options></select> and also the examples in the reply. It may help you on this matter.

Regards,
Scorphus.
Post Reply