我正在尝试在会话变量中存储选定的下拉值,每次我打印我在其他页面上使用的变量时,它会向我显示数组的最后一个值,我从中基本上填充了我的下拉列表。 这是代码;
<?php $options = array('2CHECKOUT','PAYPAL'); ?>
<select name="payment" style="width:225px;" id ="mySelect">
<?php foreach($options as $opt) { ?>
<?php $selected = (isset($_SESSION['payment']) && $opt == $_SESSION['payment']) ? ' SELECTED' : '';
echo '<option value="'.$opt.'"' .$selected.'>'.$opt.'</option>'; ?>
<?php } ?>
<?php error_log("the session var".print_r($_SESSION['payment'])); ?>
</select> </div>
有人能帮助我吗?
答案 0 :(得分:-1)
将您的代码更改为
<?php $options = array('2CHECKOUT','PAYPAL'); ?>
<div>
<select name="payment" style="width:225px;" id ="mySelect">
<?php foreach($options as $opt) { ?>
<?php $selected = (isset($_SESSION['payment']) && $opt == $_SESSION['payment']) ? ' SELECTED' : '';
echo '<option value="'.$opt.'"' .$selected.'>'.$opt.'</option>'; ?>
<?php } ?>
</select>
<?php error_log("the session var".print_r($_SESSION['payment'])); ?>
</div>