radiations3 Posted April 12, 2012 Share Posted April 12, 2012 Hello, All i am trying to do is to set the selected value of drop down menu according to the particular value returned from the database like if person saved his gender as 'Male' and he wants to update his profile then the selected option shown on the Gender's dropdown llist should be shown as Male cause if this doesn't happen 'Poor guy becomes a female due to this small problem in my code' KINDLY HELP!!!!!!! <select name="select" id="select"> <option value="Male">Male</option> <option value="Female">Female</option> </select> Quote Link to comment https://forums.phpfreaks.com/topic/260775-issue-regarding-setting-the-value-as-selected-of-dropdownmenu/ Share on other sites More sharing options...
NomadicJosh Posted April 12, 2012 Share Posted April 12, 2012 I don't know what your php code looks like, but his should get you started and this is something that I've used in the past: <select name="gender" id="select"> <?php $result = $db->query("SELECT * FROM " . DB . "members WHERE username = '" . $_GET['username'] . "'"); while($row = $result->fetch_array()) { if($user->get_user_info($_GET['username'],'gender') == $row['gender_code']) { ?> <option value="<?php echo $row['gender_code'];?>" selected><?php echo $row['gender_name']; ?></option> <?php } else { ?> <option value="<?php echo $row['gender_code'];?>"><?php echo $row['gender_name']; ?></option> <?php } } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/260775-issue-regarding-setting-the-value-as-selected-of-dropdownmenu/#findComment-1336584 Share on other sites More sharing options...
chriscloyd Posted April 12, 2012 Share Posted April 12, 2012 what if he does not have a class <?php session_start(); //include your config file to connect to db //get id or name or how ever you set ur where statement //for example ill do a session $id = $_SESSION['MySiteUserId']; $Sql = "SELECT `gender` FROM `Users` WHERE `id` = '{$id}'"; $Query = mysql_query($Sql); if ($Query) { $info = mysql_fetch_array($Query); $gender = $info['gender']; } //set array for dropdown $DropDown = array ( "Male", "Female" ); echo "<select name=\"gender\">"; foreach ($DropDown as $val) { if ($val == $gender) { echo "<option value=\"{$val}\" selected>{$val}</option>"; } else { echo "<option value=\"{$val}\">{$val}</option>"; } } echo "</select>"; Quote Link to comment https://forums.phpfreaks.com/topic/260775-issue-regarding-setting-the-value-as-selected-of-dropdownmenu/#findComment-1336585 Share on other sites More sharing options...
radiations3 Posted April 12, 2012 Author Share Posted April 12, 2012 Thanks for helping me ouit what i am trying to do is My dropdown list isalready being defined and i want to set that option as selected which is equal to the value being returned from the database this could be done via javascripting too but i am getting problems with both approach as i am not a pro. i tried to echo as selected on the returned value but it make that value appears twice in the list so kindly help..... Quote Link to comment https://forums.phpfreaks.com/topic/260775-issue-regarding-setting-the-value-as-selected-of-dropdownmenu/#findComment-1336639 Share on other sites More sharing options...
chriscloyd Posted April 12, 2012 Share Posted April 12, 2012 Show us your code, because as of right now. What I created and posted would work.... Quote Link to comment https://forums.phpfreaks.com/topic/260775-issue-regarding-setting-the-value-as-selected-of-dropdownmenu/#findComment-1336868 Share on other sites More sharing options...
Masna Posted April 12, 2012 Share Posted April 12, 2012 Thanks for helping me ouit what i am trying to do is My dropdown list isalready being defined and i want to set that option as selected which is equal to the value being returned from the database this could be done via javascripting too but i am getting problems with both approach as i am not a pro. i tried to echo as selected on the returned value but it make that value appears twice in the list so kindly help..... Are... Are you serious? Quote Link to comment https://forums.phpfreaks.com/topic/260775-issue-regarding-setting-the-value-as-selected-of-dropdownmenu/#findComment-1336870 Share on other sites More sharing options...
radiations3 Posted April 13, 2012 Author Share Posted April 13, 2012 <select name="Gender" id="Gender"> <option selected="selected"><?php echo $row_Recordset1['Gender']; ?></option> <option value="Male">Male</option> <option value="Female">Female</option> </select> Quote Link to comment https://forums.phpfreaks.com/topic/260775-issue-regarding-setting-the-value-as-selected-of-dropdownmenu/#findComment-1336934 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.