dtqu72830 2017-03-28 05:13
浏览 36
已采纳

如何在codeigniter中插入具有多个无线电名称的单选按钮值?

I'm trying to making online quiz with codeigniter where there is a dynamic quiz with some subject. User can add the question and the multiple choice answer in each question. How can i get the value of each answer in each multiple choice answer on every question?

<?php echo form_open_multipart('c_kuis/addKuisAnswer'); ?>
       <?php foreach($soalKuis as $row){?>
            <?php $jawab_array = array($row->pil_a,$row->pil_b,$row->pil_c,$row->pil_d);
            ?>
            <p><?=$row->soal?></p>
            <input type="radio" name="jawaban<?=$row->id_soal?>" value="A">A.<?=$jawab_array[0]?></input><br>
            <input type="radio" name="jawaban<?=$row->id_soal?>" value="B">B.<?=$jawab_array[1]?></input><br>
            <input type="radio" name="jawaban<?=$row->id_soal?>" value="C">C.<?=$jawab_array[2]?></input><br>
            <input type="radio" name="jawaban<?=$row->id_soal?>" value="D">D.<?=$jawab_array[3]?></input><br>
            <input type="hidden" name="id_soal_kuis" value="<?=$row->id_soal?>"></input>
            <br><br>
       <?php } ?>
        <button class="btn btn-primary" type="submit">Submit</button>
      <?php form_close(); ?>

and this is the code looks like when i try to inspect :

            <input type="radio" name="jawaban1" value="A">A.Hamster</input><br>
            <input type="radio" name="jawaban1" value="B">B.Tiger</input><br>
            <input type="radio" name="jawaban1" value="C">C.Shark</input><br>
            <input type="radio" name="jawaban1" value="D">D.Lion</input><br>
            <br><br>

            <input type="radio" name="jawaban2" value="A">A.was</input><br>
            <input type="radio" name="jawaban2" value="B">B.have</input><br>
            <input type="radio" name="jawaban2" value="C">C.am</input><br>
            <input type="radio" name="jawaban2" value="D">D.is</input><br>
            <br><br>
                <button class="btn btn-primary" type="submit">Submit</button>

Here is my Controller looks like :

public function addKuisAnswer(){
    if($this->session->userdata('data_user')->level != 1){
        redirect('/c_user/login');
    }
    $id_soal_kuis = $_POST['id_soal_kuis'];
    $id_user = $this->session->userdata('data_user')->id_user;
    $answer = $_POST['jawaban']; //The Problem's here... I should put jawaban with id_soal. Because the answer named 'jawaban1','jawaban2' etc it will follow the id_soal. How can i insert it?
    $this->load->model('m_kuis');
    $this->m_kuis->createAnswerKuis($id_soal_kuis, $id_user, $answer);
     echo " <script>
                    alert('Jawaban Tugas Tersimpan!');
                    history.go(-2);
                    </script>"; 

}

Here is my model :

public function createAnswerKuis($id_soal_kuis, $id_user, $answer){
    $data = array(
        'id_soal_kuis'=>$id_soal_kuis,
        'id_user' => $id_user,
        'jawaban_kuis' => $answer            
    );
    $this->db->set('tgl_jawab', 'NOW()', FALSE);
    $this->db->insert('jawab_kuis', $data);
}

I want the table looks like this :This is the picture of database table i made.I want to insert the data like this.

The problem is in Controller. How can my Controller post the value of radio button name="jawaban<?=$row->id_soal?>"in database?

  • 写回答

5条回答 默认 最新

  • dongqiao1151 2017-03-29 05:51
    关注

    i hope this little change will helps you

     public function addKuisAnswer(){
            if($this->session->userdata('data_user')->level != 1){
                redirect('/c_user/login');
            }
            $id_soal_kuis = $_POST['id_soal_kuis'];
            $id_user = $this->session->userdata('data_user')->id_user;
    
            $this->load->model('m_kuis');
            $i=1;
        while(isset($_POST['jawaban'.$i]))
        {
             $answer = $_POST['jawaban'.$i];
    //         var_dump($answer);
     $this->m_kuis->createAnswerKuis($id_soal_kuis, $id_user, $answer);
             $i++;
        }
    
             echo " <script>
                            alert('Jawaban Tugas Tersimpan!');
                            history.go(-2);
                            </script>"; 
    
        }
    

    you can change radio button name in sequential order(because there is no need of id appending in radio button)

     <?php 
    $i=1;
        foreach($soalKuis as $row){?>
                <?php $jawab_array = array($row->pil_a,$row->pil_b,$row->pil_c,$row->pil_d);
                ?>
                <p><?=$row->soal?>
        <input type="hidden" value=<?php echo $row->id_soal?> name='id_soal'<?= $i ?>/></p>
                <input type="radio" name="jawaban<?=$i?>" value="A">A.<?=$jawab_array[0]?></input><br>
                <input type="radio" name="jawaban<?=$i?>" value="B">B.<?=$jawab_array[1]?></input><br>
                <input type="radio" name="jawaban<?=$i?>" value="C">C.<?=$jawab_array[2]?></input><br>
                <input type="radio" name="jawaban<?=$i?>" value="D">D.<?=$jawab_array[3]?></input><br>
                <input type="hidden" name="id_soal_kuis" value="<?=$row->id_soal?>"></input>
                <br><br>
           <?php $i++;} ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥50 关于#html5#的问题:H5页面用户手机返回的时候跳转到指定页面例如(语言-javascript)
  • ¥15 无法使用此凭据登录,因为你的域不可用,如何解决?(标签-Windows)
  • ¥15 yolov9的训练时间
  • ¥15 二叉树遍历没有报错但无法正常运行
  • ¥15 在linux系统下vscode运行robocup3d上场球员报错
  • ¥15 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿
  • ¥15 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动