vinpkl Posted December 5, 2008 Share Posted December 5, 2008 hi all i have a registration form with <select> dropdown with value of 50 cities in it. These values are not coming from database. these are static values. <select> <option value="city1">city1</option> <option value="city2">city2</option> <option value="city3">city3</option> <option value="city4">city4</option> <option value="city5">city5</option> upto <option value="city50">city50</option> </select> when the user select the dropdown option value in it then the "selected value" gets added to database. Then in the site i have given an option to modify this <select> dropdown option. When the user goes to modify page then i want that the option value that he selected while registering should automatic get selected in the <select> dropdown options to show him that the "selected value" was his earlier choice that he had selected. how can dropdown automatically select the value that is in the database. vineet Quote Link to comment https://forums.phpfreaks.com/topic/135603-dropdown-problem/ Share on other sites More sharing options...
mrdamien Posted December 5, 2008 Share Posted December 5, 2008 Theres no easy way. If you are not storing these cities in a database, at least make a variable to hold them all: $cities = array( 'city1', 'city2', 'city3' /// etc ); Then you can loop through them all and decide to select one, or not. $users_city = "city3"; $html = array(); foreach($cities as $city){ if($users_city == $city){ $html[] = "<option selected=\"selected\" value=\"$city\">$city</option>"; }else{ $html[] = "<option value=\"$city\">$city</option>"; } } echo "<select>" . implode("\n", $html) . "</select>"; Edit: Also forgot to mention another method, which I do not recommend. Simply take your list, and add this bit of javascript. It works for most browsers. <select id="theSelectID"> <option value="city1">city1</option> <option value="city2">city2</option> <option value="city3">city3</option> <option value="city4">city4</option> <option value="city5">city5</option> upto <option value="city50">city50</option> </select> <script> document.getElementById('theSelectID').value = '<?php echo $theUsersCity; ?>'; </script> Quote Link to comment https://forums.phpfreaks.com/topic/135603-dropdown-problem/#findComment-706484 Share on other sites More sharing options...
chronister Posted December 5, 2008 Share Posted December 5, 2008 Take your city list and make it an array. Once it is an array, you will use it to create your dropdown list. In the loop that creates your dropdown list, you will have to compare the values to the current item in the loop. mrdamien posted before I got through typing this out, but I feel mine is a little cleaner. (no offense mrdamien) <?php $cities = array('city1', 'city2', 'city3'); $previouslySelected = 'city3'; echo '<select name="cities">'; foreach($cities as $city) { if($previouslySelected == $city) { echo '<option value="'.$city.'" selected="selected">'.$city.'</option>'; } else { echo '<option value="'.$city.'" >'.$city.'</option>'; } echo '</select>'; } ?> *edit hahaha... mrdamien we both had similar ideas and both used city3 as the "previously selected" item. Quote Link to comment https://forums.phpfreaks.com/topic/135603-dropdown-problem/#findComment-706488 Share on other sites More sharing options...
.josh Posted December 5, 2008 Share Posted December 5, 2008 chronister you put the closing select inside the foreach it needs to be outside the loop. Anyways, I win. <?php $cities = array('city1', 'city2', 'city3'); $previouslySelected = 'city3'; echo "<select name='cities'>"; foreach($cities as $city) { $selected = ($previouslySelected == $city)? " selected='selected'" : ""; echo "<option value='$city'$selected>$city</option>"; } echo "</select>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/135603-dropdown-problem/#findComment-706497 Share on other sites More sharing options...
vinpkl Posted December 5, 2008 Author Share Posted December 5, 2008 chronister you put the closing select inside the foreach it needs to be outside the loop. Anyways, I win. <?php $cities = array('city1', 'city2', 'city3'); $previouslySelected = 'city3'; echo "<select name='cities'>"; foreach($cities as $city) { $selected = ($previouslySelected == $city)? " selected='selected'" : ""; echo "<option value='$city'$selected>$city</option>"; } echo "</select>"; ?> hi all at the time of registering i requested the customer_city with $customer_city=$_REQUEST['customer_city']; so in your code i will have to change the city3 to $customer_city or anything else also. i dont have much knowledge of arrays. please clarify <?php $cities = array('city1', 'city2', 'city3'); $previouslySelected = '$customer_city'; echo "<select name='cities'>"; foreach($cities as $city) { $selected = ($previouslySelected == $city)? " selected='selected'" : ""; echo "<option value='$city'$selected>$customer_city</option>"; } echo "</select>"; ?> vineet Quote Link to comment https://forums.phpfreaks.com/topic/135603-dropdown-problem/#findComment-706503 Share on other sites More sharing options...
chronister Posted December 5, 2008 Share Posted December 5, 2008 chronister you put the closing select inside the foreach it needs to be outside the loop. Anyways, I win. DOOOHHHH...I am catching up on HEROES and typing intermittently. Stupid error. Oh well, I could never compete with you Craon Violent, not in your code or your wit. Was reading through your random quotes on your site. Freaking genius Quote Link to comment https://forums.phpfreaks.com/topic/135603-dropdown-problem/#findComment-706504 Share on other sites More sharing options...
chronister Posted December 5, 2008 Share Posted December 5, 2008 The var $previouslySelected was just a placeholder for the var you use for the previously selected item. I have replaced it in the code... <?php $cities = array('city1', 'city2', 'city3'); echo "<select name='cities'>"; foreach($cities as $city) { $selected = ($customer_city== $city)? " selected='selected'" : ""; echo "<option value='$city'$selected>$customer_city</option>"; } echo "</select>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/135603-dropdown-problem/#findComment-706507 Share on other sites More sharing options...
vinpkl Posted December 5, 2008 Author Share Posted December 5, 2008 The var $previouslySelected was just a placeholder for the var you use for the previously selected item. I have replaced it in the code... <?php $cities = array('city1', 'city2', 'city3'); echo "<select name='cities'>"; foreach($cities as $city) { $selected = ($customer_city== $city)? " selected='selected'" : ""; echo "<option value='$city'$selected>$customer_city</option>"; } echo "</select>"; ?> hi i have tried it and not able to understand why is it giving me a blank dropdown with no values vineet Quote Link to comment https://forums.phpfreaks.com/topic/135603-dropdown-problem/#findComment-706518 Share on other sites More sharing options...
chronister Posted December 5, 2008 Share Posted December 5, 2008 Try that, it works for me. <?php $cities = array('city1', 'city2', 'city3'); echo "<select name='cities'>"; foreach($cities as $city) { $selected = ($customer_city== $city)? " selected='selected'" : ""; echo '<option value="'.$city.'" '.$selected.'>'.$city.'</option>'; } echo "</select>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/135603-dropdown-problem/#findComment-706521 Share on other sites More sharing options...
vinpkl Posted December 5, 2008 Author Share Posted December 5, 2008 Try that, it works for me. <?php $cities = array('city1', 'city2', 'city3'); echo "<select name='cities'>"; foreach($cities as $city) { $selected = ($customer_city== $city)? " selected='selected'" : ""; echo '<option value="'.$city.'" '.$selected.'>'.$city.'</option>'; } echo "</select>"; ?> hi chronister thanks. this one works for me also. vineet Quote Link to comment https://forums.phpfreaks.com/topic/135603-dropdown-problem/#findComment-706524 Share on other sites More sharing options...
.josh Posted December 5, 2008 Share Posted December 5, 2008 The var $previouslySelected was just a placeholder for the var you use for the previously selected item. I have replaced it in the code... <?php $cities = array('city1', 'city2', 'city3'); echo "<select name='cities'>"; foreach($cities as $city) { $selected = ($customer_city== $city)? " selected='selected'" : ""; echo "<option value='$city'$selected>$customer_city</option>"; } echo "</select>"; ?> hi i have tried it and not able to understand why is it giving me a blank dropdown with no values vineet It didn't work because you used the wrong variable: $customer_city Quote Link to comment https://forums.phpfreaks.com/topic/135603-dropdown-problem/#findComment-706552 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.