I’m new to PHP and would love to ask about how to get multiple values on checkbox based on the question number. I provide a screenshot of a program that i working to.
Question image
This is the PHP generating HTML :
<?php $i = 1; ?>
<?php foreach ($dua as $d) : ?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $d['nama_soal'] ?></td>
<td><img style="border-radius: 5px; width: 375px; height: auto;" src="<?php echo base_url('assets/dashboard/images/soalcfit/subtest2/' . $d['img_soal'] . '.jpg'); ?>" alt=""></td>
<td>
<input type="checkbox" class="checkoption<?php echo $d['id'] ?>" name="jsub2[]" value="A"> A
<input type="checkbox" class="checkoption<?php echo $d['id'] ?>" name="jsub2[]]" value="B"> B
<input type="checkbox" class="checkoption<?php echo $d['id'] ?>" name="jsub2[]" value="C"> C
<input type="checkbox" class="checkoption<?php echo $d['id'] ?>" name="jsub2[]" value="D"> D
<input type="checkbox" class="checkoption<?php echo $d['id'] ?>" name="jsub2[]" value="E"> E
</td>
</tr>
<?php $i++; ?>
<?php endforeach; ?>
This is the Controller for submitting the post :
public function c2_proses() {
if ($this->input->post()) {
//input per subtest
$jsub2 = $this->input->post('jsub2');
//untuk foreach soal
$soal2 = $this->CfitModel->getDataAllSoalSubDua();
//variabel id user
$id_user = $_SESSION['user_id'];
//foreach kombinasi sub-1
foreach($soal2 as $key => $s2)
{
$this->CfitModel->inputValue($id_user, $s2['id'], $jsub2[$key]);
}
redirect('ujian/csub2');
}
}
This is the Model :
public function inputValue($id_user, $id_soal, $jawaban) {
return $this->db->insert('cfit_hasil', array('id_user' => $id_user, 'id_soal' => $id_soal, 'jawaban' => $jawaban));
}
For example i choose A,B from Question number one, and C, D for Question number two, but the output is like this :
- A
- B
- C
- D
I need my output on the database like :
- AB
- CD, etc.
Muhammad Zamzamy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2