How do I insert a radio button value that has multiple radio names in codeigniter?
I am trying to make an online survey using codeigniter where there is a dynamic survey with some subject. User can add multiple choice question and answer to each question. How can I get the meaning of each answer in each multiple choice answer for each 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 code looks like when I try to check:
<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's my controller looks like this:
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 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 to look like this: This is an image of the database table i. I want to insert data like this.
The problem is in the controller. How can my controller specify the value of a radio button name="jawaban<?=$row->id_soal?>"
in the database?
source to share
Hope this little change 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 the name of the radio button in sequential order (because there is no need to add id to the 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++;} ?>
source to share
This is the question you had in mind:
How can I insert the database value name = "jawabanid_soal?>" Into the database?
or, how to read the value of the selected radio button and then insert it into the DB?
I will answer both. To read the meaning of the answers, you can go with a loop foreach
:
<?php
$egabat= array();
foreach($soalKuis as $row){
$egabat[$row->id_soal]= $_POST["jawaban".$row->id_soal];
} ?>
To insert a value into a DB if it is MySql I recommend this very helpful guide.
source to share
You can put the $ row-> id_soal value in the hidden field of the array name when submitting the form, then get the values of the radio button with dynamic ids that come from the hidden field.
<?php
$egabat= array();
foreach($_POST['hiddenfield'] as $idsoal){
$egabat[$idsoal]= $_POST["jawaban".$idsoal];
} ?>
source to share
Here's how to do it:
<?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 in the controller you will have these values in an array which can be retrieved like this:
<?php
foreach($this->input->post('jawaban') as $radio){
echo $radio;
}
source to share