mckeegan375 Posted December 23, 2010 Share Posted December 23, 2010 Hi I have been trying to get a value to be selected in a mysql populated dropdown list but can't get it to work and was hoping someone could help I have a database with user info in it and this is an update page where they can update their details. The code i have (which doesn't work) is: <select name="agency"> <? $query1 = mysql_query("SELECT * FROM agents ORDER BY agent ASC",$connect); while($myrow = mysql_fetch_assoc($query1)){ $agent = $myrow['agent']; echo "<option"; if ($agent == $agency) { echo "selected='selected'"; } echo ">$agent</option>"; } ?> </select> The $agency value is the current agency which is stored in the users profile and the value does exist in the list which is being populated (also, i have define $agency further up in my code) so i don't know why the selected value won't display. No value is displayed in the dropdown list on the page - but the values are in the list if i remove the selected='selected' part of the code. Any help yould be greatly appreciated. Merry Christmas Andy Link to comment https://forums.phpfreaks.com/topic/222527-default-selected-value-in-dropdown/ Share on other sites More sharing options...
BlueSkyIS Posted December 23, 2010 Share Posted December 23, 2010 you probably need to add space before the selected echo " selected='selected'"; Look at your HTML and you'll probably see no space before selected, invalidating the HTML Link to comment https://forums.phpfreaks.com/topic/222527-default-selected-value-in-dropdown/#findComment-1150899 Share on other sites More sharing options...
mckeegan375 Posted December 23, 2010 Author Share Posted December 23, 2010 Wow! fast response and perfect solution! Many thanks! Link to comment https://forums.phpfreaks.com/topic/222527-default-selected-value-in-dropdown/#findComment-1150900 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.