0

how to save radio button has been selected at the time the page is reloaded? suppose I have selected some of the radio button and then reload the browser page. but after the page is reloaded radio buttons have been still the same. I use Codeigniter.

here is my view code

<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>

here my controller code

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);
    }

How I set cookie and where I can put cookie code in controller?

agung_phe
  • 103
  • 1
  • 1
  • 8
  • Welcome to SO, could you please update your question with more relevant information like a piece of code? – Rob Feb 15 '14 at 10:24

1 Answers1

0
  1. Check which radio(s) was selected in controler (http://ellislab.com/codeigniter%20/user-guide/libraries/input.html).
  2. Pass this data to view (http://ellislab.com/codeigniter/user-guide/general/views.html).
  3. Mark radio(s) as selected within view depending on data passed by controller (Assign an initial value to radio button as checked).
Community
  • 1
  • 1
Robert Trzebiński
  • 1,327
  • 8
  • 16
  • is there a example? I don't know hot to set cookie and how to fetch it – agung_phe Feb 15 '14 at 11:03
  • What do you whant to use cookies for? Please open and read carefully provided links, everything should be clear after that. – Robert Trzebiński Feb 15 '14 at 11:15
  • yeah, now I understand, I need to save cookie for online test application, so when student refresh/reload the page it's still selected radio button... thanks for your info – agung_phe Feb 15 '14 at 11:41
  • Robert Trzebiński, I have added my code above, where I can put my setcookie code and how I load my cookie? – agung_phe Feb 15 '14 at 14:36