选择的问题=选择在php中回显时按预期运行

时间:2013-02-12 21:29:58

标签: php select selected

此代码应将selected = selected回显到相关选项,但它不会将所选值显示为突出显示,或者在源代码中显示selected = selected。

<?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();
?>
<select name="gift_privacy">
<option value="Standard" <?php if($row['gift_privacy']=='Standard') echo "selected='selected'"; ?>>Standard</option>
<option value="Gift ID Req" <?php if($row['gift_privacy']=='Gift_ID_Req') echo "selected='selected'"; ?>>Require program ID</option>
<option value="Not Enrolled" <?php if($row['gift_privacy']=='Not_Enrolled') echo     </select>

var_dump($row);的结果:

["gift_privacy"]=> string(12) "Not Enrolled"

源代码

<select name="gift_privacy" style="width:12em;">
<option value="Standard" >Standard</option>
<option value="Gift ID Req" >Require program ID</option>
<option value="Not Enrolled" >Do not enroll</option>
</select>

2 个答案:

答案 0 :(得分:4)

你的条件是:

if($row['gift_privacy']=='Not_Enrolled')

但你的字符串是“未注册”(没有下划线)。要么更改条件并删除下划线,要么将要返回的值更改为下划线。

答案 1 :(得分:3)

您的if语句显示$row['gift_privacy']=='Not_Enrolled',而您的var_dump输出显示为["gift_privacy"]=> string(12) "Not Enrolled"。请注意Not和Enrolled之间的下划线(_)。