【问题标题】:how to save radio button has been selected at the time the page is reloaded?重新加载页面时如何保存单选按钮?
【发布时间】:2014-03-14 19:26:11
【问题描述】:

如何在页面重新加载时保存单选按钮?假设我选择了一些单选按钮,然后重新加载浏览器页面。但是页面重新加载后单选按钮还是一样的。我使用 Codeigniter。

这是我的视图代码

<div class="container">
<?php
$no=1;
foreach($hasil->result() as $row):
?>
<form action="<?php echo base_url();?>mahasiswa/hasil" method="post" class="form" enctype="multipart/form-data" name="form" onsubmit="stopCounter();">
<input type="hidden" name="id_soal[<?php echo $row->id_soal;?>]" value="<?php echo $row->id_soal;?>" />
<table>
  <tr>
    <td colspan="2">&nbsp;</td>
  </tr>
  <tr>
    <td><b><?php echo $no.'.&nbsp;';?></b></td>
    <td><?php echo $row->pertanyaan;?></td>
  </tr>
  <tr>
    <td rowspan="5">&nbsp;</td>
    <td><span class="radio"><label><input type="radio" name="jawab[<?php echo $row->id_soal;?>]" value="A" />a.&nbsp;<?php echo $row->a;?></label></span></td>
  </tr>
  <tr>
    <td><span class="radio"><label><input type="radio" name="jawab[<?php echo $row->id_soal;?>]" value="B" />b.&nbsp;<?php echo $row->b;?></label></span></td>
  </tr>
  <tr>
    <td><span class="radio"><label><input type="radio" name="jawab[<?php echo $row->id_soal;?>]" value="C" />c.&nbsp;<?php echo $row->c;?></label></span></td>
  </tr>
  <tr>
    <td><span class="radio"><label><input type="radio" name="jawab[<?php echo $row->id_soal;?>]" value="D" />d.&nbsp;<?php echo $row->d;?></label></span></td>
  </tr>
  <tr>
    <td><span class="radio"><label><input type="radio" name="jawab[<?php echo $row->id_soal;?>]" value="E"/>e.&nbsp;<?php echo $row->e;?></label></span></td>
  </tr>
<?php
$no++;
?>
<input type="hidden" name="id_sesi" value="<?php echo $row->id_sesi;?>" />
<input type="hidden" name="jumlah" value="<?php echo $jumlah;?>" />
<?php
endforeach;
?>
  <tr>
    <td colspan="2">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="2"><input type="submit" value="Selesai" class="btn btn-default"/></td>
  </tr>
</table>
</form>
</div>

这里是我的控制器代码

public function mulai_tes(){
        $id=$this->uri->segment(3);
        $this->load->model('m_mahasiswa');
        $result=$this->m_mahasiswa->data_mahasiswa();
        foreach($result->result() as $row):
            $data['user']=$row->username;
            $data['nama']=$row->nama;
        endforeach;
        $cek=$this->m_mahasiswa->validasi_tes($id,$data['user']);
        foreach($cek->result() as $c){
            if($c->id_sesi==$id){ ?>
                <script type="text/javascript" language="javascript">
                alert("Anda telah mengikuti tes soal ini");
                </script>
        <?php
        echo "<meta http-equiv='refresh' content='0; url=".base_url()."mahasiswa/tes'>";
            }
        }
        $data['hasil']=$this->m_mahasiswa->mulaites($id);
        $data['jumlah']=$data['hasil']->num_rows();
        $data['judul']='Mulai Tes';
        $this->load->view('elearning/template',$data);
    }

我如何设置 cookie 以及我可以将 cookie 代码放在控制器中的什么位置?

【问题讨论】:

  • 欢迎来到 SO,您能否用更相关的信息(例如一段代码)更新您的问题?

标签: php codeigniter cookies radio-button reload


【解决方案1】:
  1. 检查在控制器中选择了哪些无线电 (http://ellislab.com/codeigniter%20/user-guide/libraries/input.html)。
  2. 将此数据传递给查看 (http://ellislab.com/codeigniter/user-guide/general/views.html)。
  3. 根据控制器 (Assign an initial value to radio button as checked) 传递的数据,在视图中将无线电标记为选中。

【讨论】:

  • 有例子吗?我不知道如何设置 cookie 以及如何获取它
  • 你想用cookies做什么?请打开并仔细阅读提供的链接,之后一切都应该清楚了。
  • 是的,现在我明白了,我需要为在线测试申请保存 cookie,所以当学生刷新/重新加载页面时,它仍然是选中的单选按钮...感谢您的信息
  • Robert Trzebiński,我在上面添加了我的代码,我可以在哪里放置我的 setcookie 代码以及如何加载我的 cookie?
猜你喜欢
  • 1970-01-01
  • 2017-04-30
  • 1970-01-01
  • 2019-10-26
  • 1970-01-01
  • 2014-01-07
  • 2018-12-18
  • 2021-12-08
相关资源
最近更新 更多