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??

Link to comment
Share on other sites

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>';
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.