jjmusicpro Posted May 27, 2008 Share Posted May 27, 2008 i was pulling a value from mysql, and i wanted to put that value into the drop down. the drop down has 3 values seattle, phoenix, and new york. the value from the drop down is always going to match one pulled from db.. so... if the value pulled from the db is say seattle, i wanted seattle to be selected in the drop down menu, but they can change it if they want, i just want it to be selected when they go to the page, with the values that match it form the db Link to comment https://forums.phpfreaks.com/topic/107495-default-a-value-in-drop-down/ Share on other sites More sharing options...
GingerRobot Posted May 27, 2008 Share Posted May 27, 2008 How about showing us the code you have? It'll be a simple case of adding selected="selected" to the relevant option through the use of an if statement, but if we knew the context we could help more. Link to comment https://forums.phpfreaks.com/topic/107495-default-a-value-in-drop-down/#findComment-550995 Share on other sites More sharing options...
jjmusicpro Posted May 27, 2008 Author Share Posted May 27, 2008 how do i make it so it wont write out that value again... so i pull "seattle" form the guys profile, and i make a form that i wanted to edit his info. so the form will have a select drop down box, with 3 values seattle, phoenix, and new york. instead of the drop down box starting off with phoenix or some other place, i wanted it to show the value selected as the one in the db. Link to comment https://forums.phpfreaks.com/topic/107495-default-a-value-in-drop-down/#findComment-551024 Share on other sites More sharing options...
runnerjp Posted May 27, 2008 Share Posted May 27, 2008 yes like ginger said selected="selected" Link to comment https://forums.phpfreaks.com/topic/107495-default-a-value-in-drop-down/#findComment-551027 Share on other sites More sharing options...
jjmusicpro Posted May 27, 2008 Author Share Posted May 27, 2008 i tried setting an option value as you said but it didnt work <select name=\"payscale\"> <option selected=\"$payscale\"></option> <option value=\"seattle\">seattke</option> <option value=\"phoenix\">phoenix</option> <option value=\"new york\">new york</option> </select> Link to comment https://forums.phpfreaks.com/topic/107495-default-a-value-in-drop-down/#findComment-551040 Share on other sites More sharing options...
jjmusicpro Posted May 27, 2008 Author Share Posted May 27, 2008 anyone? ??? Link to comment https://forums.phpfreaks.com/topic/107495-default-a-value-in-drop-down/#findComment-551084 Share on other sites More sharing options...
Darklink Posted May 27, 2008 Share Posted May 27, 2008 No no. <select name=\"payscale\"> <option selected=\"selected\"></option> <option value=\"seattle\">seattke</option> <option value=\"phoenix\">phoenix</option> <option value=\"new york\">new york</option> </select> It must be selected="selected" Link to comment https://forums.phpfreaks.com/topic/107495-default-a-value-in-drop-down/#findComment-551094 Share on other sites More sharing options...
jjmusicpro Posted May 27, 2008 Author Share Posted May 27, 2008 i pull the variable from the db and it $selected = 'seattle" i tried to put your code below but it dosent work, it dosnt pull back info in selected list. Link to comment https://forums.phpfreaks.com/topic/107495-default-a-value-in-drop-down/#findComment-551162 Share on other sites More sharing options...
jonsjava Posted May 27, 2008 Share Posted May 27, 2008 something like this: print "<select name='payscale'> <option value='' selected='selected'></option> while ($row = mysql_fetch_assoc($result){ $city = $row['city']; print "<option value='$city'>$city</option>\n"; } print "</select>"; Link to comment https://forums.phpfreaks.com/topic/107495-default-a-value-in-drop-down/#findComment-551168 Share on other sites More sharing options...
BlueSkyIS Posted May 27, 2008 Share Posted May 27, 2008 can we see your latest code? Link to comment https://forums.phpfreaks.com/topic/107495-default-a-value-in-drop-down/#findComment-551169 Share on other sites More sharing options...
jjmusicpro Posted May 27, 2008 Author Share Posted May 27, 2008 my cities are hardcoded on the page, not in the database so i wanted the selected value to be the value retrieved in the db. Link to comment https://forums.phpfreaks.com/topic/107495-default-a-value-in-drop-down/#findComment-551181 Share on other sites More sharing options...
GingerRobot Posted May 28, 2008 Share Posted May 28, 2008 Try: <?php $cities = array('seattle','pheonix','new york');//array of cities //assuming $payscale is the variable containing the state name: echo '<select name="payscale">'; foreach($cities as $v){ $selected = ''; if($v == $payscale){ $selected = ' selected="selected" '; } echo '<option value="'.$v.'"'.$selected,'>'.$v.'</option>'; } echo '</select>'; ?> Link to comment https://forums.phpfreaks.com/topic/107495-default-a-value-in-drop-down/#findComment-551417 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.