an option already selected on page load in drop down list and also changeble [duplicate]

Possible Duplicate:
PHP - PRE-select drop down option

I have a situation in my php project where a user can edit his account.

In editing, there is a city field which is a drop down list of cities.

I want to already select one city which is in profile before editing(the one which user enter at the time of registration).

Also he is able to change his city by choosing fron drop down list.

Html code:

<div class="search_bar1_txt">State:</div>
<div class="search_bar1">
<select class="styled" name="state_trainer">

on editing I am using this code to fetch current data(city) of user:

$sql = "select city from `wp_pelleresuser` where userId =".$_GET['userid'];
$result = mysql_query($sql);
$value = mysql_fetch_assoc($result);

please tell me how can I get already selected one option which is fetch from database.

And also it is changeable.



You can do it in a number of ways, the simplest (and probably least elegant) is to do something like this:

<select class="styled" name="state_trainer">
$myCity='london'; // assumed to be data from database...
echo '<option'.($myCity=='-select-') ? ' selected ' : ' ' .'-select-</option>';
echo '<option'.($myCity=='washington') ? ' selected ' : ' ' .'washington</option>';

This is of course horrid.

I would rather suggest that you check the data as you are pulling your information out of the database and putting your initial dropdown list together.

If you are making an array of data to create the drop down list (for example) check it right there and then. If the city matches what you want, do it inside your loop right off the bat.


while( ... ) // Database loop that is pulling the data from the database.
        $selected=' selected ';

Then to display the drop down list, you can simply do this:

    echo $myCityList[$i].'\n';

The users city will already be selected.

<div class="search_bar1_txt">State:</div>
 <div class="search_bar1">
  <select class="styled" name="state_trainer">
   <option <?if($value['city']=='-select-') echo "selected";?>>-select-</option>
   <option <?if($value['city']=='washington') echo "selected";?>>washington</option>
   <option <?if($value['city']=='perth') echo "selected";?>>perth</option>
   <option <?if($value['city']=='delhi') echo "selected";?>>delhi</option>
   <option <?if($value['city']=='london') echo "selected";?>>london</option>

I'm not sure if it is clear way but you can put code inside every option cell like this:

<option <?php if($value['city'] == "washington") echo "selected=selected"; ?> >washington</option>

This way you can get what u want. And better use mysqli function for database interactions.

<?php if ($value['city'] == delhi)
                                echo "selected = true"; 

          }  ?> 

Just het the selected value form db like

$choosen = $some value form db

then $options = array(1 => 'data1', 2 => 'data2', 3 => 'data3'); foreach ($options as $key => $value) { echo '' . $value . ''; }

to get your selection box in php.I thnk you understand,it works for me


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.