0

我希望单选按钮在所有分页中保持选中状态。为了

我已经在页面顶部的分页中收集了检查值。

但它运作不佳。

这段代码有什么问题?

这就是我在会话中获取单选按钮的选定值的方式

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"; }?> />

它给了我错误(通知),比如答案是未定义的索引。

4

3 回答 3

3

isset() 返回真或假。您的 if 语句应该是,例如:

if (isset($_SESSION['answer']) && $_SESSION['answer'] == 'yes')
于 2013-09-23T07:29:19.530 回答
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
于 2013-09-23T07:44:14.793 回答
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 . '/>';
}
于 2013-09-23T07:31:29.720 回答