Andy17 Posted October 3, 2008 Share Posted October 3, 2008 Hey guys, I was wondering how I can get my dropdown box to select a certain value based on a variable. If you do not know what I mean, let me give you an example: <?php $category = "Classic"; ?> <form name="blabla" method="post"> <select name="category"> <OPTION>Adult</OPTION> <OPTION>Alcohol</OPTION> <OPTION>Bar</OPTION> <OPTION>Blonde</OPTION> <OPTION>Brunette</OPTION> <OPTION>Classic</OPTION> <OPTION>Men</OPTION> <OPTION>Sports</OPTION> <OPTION>Women</OPTION></select> </form> In the example above, I would like "Classic" to be preselected in the dropdown box when the page loads, because $category = "Classic". If $category was = "Blonde", then I would obviously want "Blonde" to be preselected. Is there a simple way to do this? Thank you! Link to comment https://forums.phpfreaks.com/topic/126896-solved-dropdown-selection-from-variable/ Share on other sites More sharing options...
aebstract Posted October 3, 2008 Share Posted October 3, 2008 <OPTION <?php if($var == adult){ echo selected="selected"; } ?>>Adult</OPTION> <OPTION>Alcohol</OPTION> <OPTION>Bar</OPTION> <OPTION>Blonde</OPTION> <OPTION>Brunette</OPTION> <OPTION>Classic</OPTION> <OPTION>Men</OPTION> <OPTION>Sports</OPTION> <OPTION>Women</OPTION> Something like that going all the way down should do it, I believe. :] Link to comment https://forums.phpfreaks.com/topic/126896-solved-dropdown-selection-from-variable/#findComment-656330 Share on other sites More sharing options...
Andy17 Posted October 3, 2008 Author Share Posted October 3, 2008 Oh yeah, I thought about that as well, but I just figured there would be a smarter way to do it (one that takes up less code). I'll just try it anyways. Thanks! Link to comment https://forums.phpfreaks.com/topic/126896-solved-dropdown-selection-from-variable/#findComment-656331 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.