Default value in drop down
I am creating a edit user page which contains text boxes and drop down menus populated from data within the database.
I currently call a function to populate the drop down menus. For example, here is the code that I used to populate a drop down:
<select name="manager">;
<?php
// printing the list box select command
foreach($managerDetails as $row){
//Array or records stored in $managerDetails
echo "<option value='${row['userID']}'>
${row['lastName']}, ${row['fi开发者_Go百科rstName']}
</option>";
/* Option values are added by looping through the array */
}
?>
</select>
I am attempting to have the combo box automatically display the value from the database. I have attempted placing this as the value of :
value="<?php echo $userDetails['managerID']; ?>"
Thanks.
echo "<option value='${row['userID']}' ". (($userDetails['managerID'] == $row['userID']) ? "selected='selected'":"").">${row['lastName']}, ${row['firstName']}</option>";
will do
You want to say:
<option selected="selected">DefaultValue</Option>
here is a great example:
http://www.tizag.com/htmlT/htmlselect.php
<select name="manager">
<?php
$user_id = //the current user id selected from the database/or can get it through a login SESSION['']
foreach($managerdetails as $row=>$d){ ?>
<option <?php if($d == $user_id)echo "selected";?> value='${row['userID']}'><?php echo ${row['lastName']}, ${row['firstName']}?></option>
<?php } ?>
</select>
精彩评论