Jump to content

update form - setting a selected value from dropdown list that came from db


aian04

Recommended Posts

i have a dropdown list came from database, and i have update/edit form.

 


$nam = "SELECT * from member WHERE email = '" . $_SESSION['user'] . "'  ";


				$result = mysql_query($nam)or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) 
					{
				   		$fname = $row['first_name'];
				   		$lname = $row['last_name'];
				   		$email = $row['email'];
				   		$add = $row['address'];
				   		$add2= $row['address2'];
				   		$city= $row['city_id'];
				   		$country = $row['coutnry_id'];
				   		$tel = $row['telephone'];
				   		$mob = $row['mobile'];
				   		$fax = $row['fax'];
				   		
				   		
					}
$select_country = "SELECT id, country_name from country";
$select_city = "SELECT * from city";
$qcountry = mysql_query($select_country);
$qcity = mysql_query($select_city);


<select  onChange="selected(country)" id="country" name="country" >
				                  							
											                    <?php 
											                    while(list($id, $country_name) = mysql_fetch_row($qcountry)) 
											                    {  
												                    echo "<option value='" . $id ."'>$country_name</option>"; 
											                     } ?>
											                    </select>

 

my problem is, i need to display the country of a user stored in user database.

my dropdown list on the above only populates the country table from database. im trying to put like:

<select  onChange="selected(country)" id="country" name="country" selected="<?php $country ?>">

but it doesnt select the $country. anyone can explain what went wrong??

That's not how the selected attribute works - it's an attribute of the option tag. Presumably you are using a loop to generate the options? If so, you should add in something that checks of the current iteration of the loop is the same as $country. If it is, add the selected attribute. Something along the lines of:

 

foreach($options as $option){
$selected = ($option==$country) ? 'selected="selected"' : '';
echo '<option value="'.$option.'" '.$selected.'>'.$option.'</option>';
}

i got ur idea. but i chose to use if and else like

if($id==$country2){
												                    
												                    $selected = 'selected';
												                    }
												                    else {
													                    $selected = '';
													                    }
												                    
												                    
												                    echo "<option value='" . $id ."' $selected>$country_name</option>"; 

tnx ben

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.