Jump to content

Issue regarding setting the value as selected of dropdownmenu


radiations3

Recommended Posts

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>";

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

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.