简体   繁体   中英

Pre-select option matching db value without adding a new Select option

I am creating a customer account page which displays the relevant information in the database and allows customers to edit their information.

The code below retrieves and displays the customer's account information from the database and pre-selects the values matching their information correctly. My problem is specific to Select elements - instead of selecting from amongst the existing select options it's adding an extra select option with the value in the database, so that the value that's in the database is displayed twice.

Edit - For example, if the db value for 'gift_privacy' is 'Standard', the Select options are being displayed as: *Standard, Standard, Gift ID Req, Not_Enrolled* instead of *Standard, Gift ID Req, Not_Enrolled*

How can I correct this code so that selected="selected" is applied to the existing options?

<?php   
try {  
$stmt = $conn->prepare("SELECT * FROM customer_info WHERE user_id = :user_id");  
$stmt->bindValue(':user_id', $user_id); 
$stmt->execute();
}catch(PDOException $e) {echo $e->getMessage();}
$row = $stmt->fetch();
$search = array('_', ',');
$replace = array(' ', ', ');
$rows = str_replace($search, $replace, $row);
?>
<select name="gift_privacy">
<option selected="selected" value="<?php echo $rows['gift_privacy']; ?>"><?php echo $rows['gift_privacy']; ?></option>
<option value="Standard">Standard</option>
<option value="Gift_ID_Req">Require program ID</option>
<option value="Not_Enrolled">Do not enroll</option>
</select>

Try:

<?php
$gifts = array(
    "Standard" => "Standard",
    "Gift_ID_Req" => "Require Program ID",
    "Not_Enrolled" => "Do not Enroll");

$gift = $rows['gift_privacy'];
//$gift = "Gift_ID_Req";
foreach($gifts as $key => $value) {
    if($key == $gift) {
        echo '<option selected="selected" value="'. $key .'">'. $value .'</option>';
    } else {
        echo '<option value="'. $key .'">'. $value .'</option>';
    }
}
?>

instead of:

<option selected="selected" value="<?php echo $rows['gift_privacy']; ?>"><?php echo $rows['gift_privacy']; ?></option>
<option value="Standard">Standard</option>
<option value="Gift_ID_Req">Require program ID</option>
<option value="Not_Enrolled">Do not enroll</option>

Of course, there are multiple ways to skin a cat, and you can check to see if $rows['gift_privacy'] matches the current option on each line instead.

Try this . This is simple solution for your problem.

<select name="gift_privacy">
<option value="Standard" <?php if($rows['gift_privacy']=='Standard') echo "selected='selected'"; ?>>Standard</option>
<option value="Gift_ID_Req" <?php if($rows['gift_privacy']=='Gift_ID_Req') echo "selected='selected'"; ?>>Standard</option>
<option value="Not_Enrolled" <?php if($rows['gift_privacy']=='Not_Enrolled') echo "selected='selected'"; ?>>Standard</option>
</select>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM