aian04 Posted October 18, 2008 Share Posted October 18, 2008 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?? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted October 18, 2008 Share Posted October 18, 2008 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>'; } Quote Link to comment Share on other sites More sharing options...
aian04 Posted October 19, 2008 Author Share Posted October 19, 2008 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 Quote Link to comment 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.