【问题标题】:Storing radio button value in session and retrieve (Radio button should be checked after retrieving) in Codeigniter在会话中存储单选按钮值并在 Codeigniter 中检索(检索后应检查单选按钮)
【发布时间】:2017-11-19 16:35:37
【问题描述】:

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

为它使用 Session 但没有得到它。 我是 Codeigniter 的新手。

控制器文件

$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>"; ?> 

如有错误请指正

【问题讨论】:

  • print_r($checked_value),它会给出多个值作为一个数组..但是在检查过程中你没有通过 $checked_value[$quizid]。而不是你传递整个会话数组。

标签: php codeigniter session radio-button


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    • 1970-01-01
    • 2018-07-23
    相关资源
    最近更新 更多