我想在所有分页中保持选中单选按钮。为了taht
我已经在页面顶部的分页中收集了选中的值。
但它运作不佳。
此代码有什么问题?
这就是我在会话中获得单选按钮的选择值
session_start();
if(isset($_POST['answer'])) {
$_SESSION['answer'] = $_POST['answer'];
}
这是我的php代码
<input type="radio" name="answer" value="yes" <?php if(isset($_SESSION['answer'])=='yes') {echo "checked"; }?> />
<input type="radio" name="answer" value="no" <?php if(isset($_SESSION['answer'])=='no') {echo "checked"; }?> />
<input type="radio" name="answer" value="yes1" <?php if(isset($_SESSION['answer'])=='yes1') {echo "checked"; }?> />
<input type="radio" name="answer" value="yes2" <?php if(isset($_SESSION['answer'])=='yes2') {echo "checked"; }?> />
它给了我错误(通知),就像答案是未定义的索引。
答案 0 :(得分:3)
isset()返回true或false。你的if语句应该是,例如:
if (isset($_SESSION['answer']) && $_SESSION['answer'] == 'yes')
答案 1 :(得分:1)
尝试:
$values = array('yes', 'no', 'yes1', 'yes2');
foreach ( $values as $value ) {
$checked = isset($_SESSION['answer']) && $_SESSION['answer'] == $value ? 'checked="checked"' : '';
echo '<input type="radio" name="answer" value="' . $value . '" ' . $checked . '/>';
}
答案 2 :(得分:1)
<input type="radio" name="answer" value="yes" <?php if($_SESSION['answer']=='yes') { ?> checked="checked" <?php }?> />
<input type="radio" name="answer" value="no" <?php if($_SESSION['answer']=='no') { ?> checked="checked" <?php }?> />
<input type="radio" name="answer" value="yes1" <?php if($_SESSION['answer']=='yes1') { ?> checked="checked" <?php }?> />
<input type="radio" name="answer" value="yes2" <?php if($_SESSION['answer']=='yes2') { ?> checked="checked" <?php }?> />
please use this html