Question:
PHP/HTML: HELP! How to set default value for dropdown menu?
Kendall
2012-07-01 03:48:33 UTC
Hello. I have a problem. I am trying to set a default for a dropdown menu where students update their profiles.

Here is the code I have which is not working:

......









........


I would like the value they selected before to be displayed so the students doesnt have to reselect a value. Please help me out. Any help would be greatly appreciated. Thanks. :)
Three answers:
2012-07-01 04:39:07 UTC
some how you have to get of year, in this case we will say the have just submitted a form it may not be the most efficient but it works

$year = $_POST['Year'];



$ss ='selected';

if ($year=='1') $sel1=$ss;

else

if ($year=='2') $sel2=$ss;

else

if ($year=='3') $sel3=$ss;

else

$sel4=$ss;

}



J0nny3
2012-07-01 11:23:57 UTC
You just use "selected" e.g....







...would make Accountancy always appear as default. To get it in php while loop you can do something like this, i just use gender as an example cause it's quicker and shorter..



$gender = $row['gender'];



$options = array("Male", "Female",); // Or fill with all the options you have



if($gender == "Male") {

$options[0] = "selected='selected'";

}

elseif($gender == "Female"){

$options[1] = "selected='selected'";

}



Then you place $options in each of your options like this....



echo '';



... that should display the default of whatever is placed inside the mysql database



.. note the 0, 1 are the orders of the arrays, Male comes first in the array list so should be 0. if you had 5 arrays in the list you would use... 0, 1, 2, 3, 4
2012-07-01 13:18:16 UTC














Just do it in this way... ;-)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...