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>

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>

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

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

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?

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

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.