在 Codeigniter 中将单选按钮值存储在会话和检索中(检索后应选中单选按钮)



我正在获取 10 个测验问题,每个问题都有来自数据库的四个不同选项。使用分页一次显示这些数据,有 4 个选项。现在,当我在回答第一个问题后切换并返回上一个问题时,我发现单选按钮未选中。.我希望它应该被检查。

使用会话但没有得到它。我是编码点火器的新手。

控制器文件

$newdata = array(
    'ques1' => $this->input->post('quizid1'),
         'ques2' => $this->input->post('quizid2'),
         'ques3' => $this->input->post('quizid3'),
         'ques4' => $this->input->post('quizid4'),
         'ques5' => $this->input->post('quizid5'),
         'ques6' => $this->input->post('quizid6'),
         'ques7' => $this->input->post('quizid7'),
         'ques8' => $this->input->post('quizid8'),
         'ques9' => $this->input->post('quizid9'),
         'ques10' => $this->input->post('quizid10'),
);
$this->session->set_userdata('newdata');

查看文件代码

<?php    
 $checked_value = $this->session->userdata('newdata');
?>
<input type="radio" name="quizid<?=$row->quizID?>" value="<?=$ans_array[0]?
>" <?=($checked_value == 0) ? 'checked="checked"' :'' ?>>  <?php echo 
"$ans_array[0]</br>"; ?> 
<input type="radio" name="quizid<?=$row->quizID?>" value="<?=$ans_array[1]?
>" <?=($checked_value == 1) ? 'checked="checked"' :'' ?>>  <?php echo 
"$ans_array[1]</br>"; ?> 
<input type="radio" name="quizid<?=$row->quizID?>" value="<?=$ans_array[2]?
>" <?=($checked_value == 2) ? 'checked="checked"' :'' ?>>  <?php echo 
"$ans_array[2]</br>"; ?> 
<input type="radio" name="quizid<?=$row->quizID?>" value="<?=$ans_array[3]?
>" <?=($checked_value == 3) ? 'checked="checked"' :'' ?>>  <?php echo 
"$ans_array[3]</br>"; ?> 

如果我错了,请纠正我

它应该可以工作,之前使用整个会话数组来检查,而不是特定问题的值。

  <input type="radio" name="quizid<?=$row->quizID?>" value="<?=$ans_array[0]?
    >" <?=($checked_value['ques'.$row->quizID] == 0) ? 'checked="checked"' :'' ?>>  <?php echo 
    "$ans_array[0]</br>"; ?> 
    <input type="radio" name="quizid<?=$row->quizID?>" value="<?=$ans_array[1]?
    >" <?=($checked_value['ques'.$row->quizID] == 1) ? 'checked="checked"' :'' ?>>  <?php echo 
    "$ans_array[1]</br>"; ?> 
    <input type="radio" name="quizid<?=$row->quizID?>" value="<?=$ans_array[2]?
    >" <?=($checked_value['ques'.$row->quizID] == 2) ? 'checked="checked"' :'' ?>>  <?php echo 
    "$ans_array[2]</br>"; ?> 
    <input type="radio" name="quizid<?=$row->quizID?>" value="<?=$ans_array[3]?
    >" <?=($checked_value['ques'.$row->quizID] == 3) ? 'checked="checked"' :'' ?>>  <?php echo 
    "$ans_array[3]</br>"; ?> 

最新更新