Codeigniter - multiple input with loop

3.9k Views Asked by At

I wanted to make the multiple inputs form with loop and the different value for each data I fill, it seems this code is working but it was only getting 1 data to 3 times with the same value.

what I need is that I can make different value for each data in the form.

Or maybe do I need a model for this one or something? been struggle for few days here please help.

this is what i expect for the result in the form loop

enter image description here

but I want to get this Output:- enter image description here

controller.php

public function aksi_soal3(){
        $ps5 = $this->input->post('soal');
        $ps6 = $this->input->post('opsi_a');
        $ps7 = $this->input->post('opsi_b');
        $ps11 = $this->input->post('kunci_jawaban');

        $data = array(
            array(
                'soal' => $ps5,
                'opsi_a' => $ps6,
                'opsi_b' => $ps7,
                'kunci_jawaban' => $ps11
            ),
            array(
                'soal' => $ps5,
                'opsi_a' => $ps6,
                'opsi_b' => $ps7,
                'kunci_jawaban' => $ps11
            ),
            array(
                'soal' => $ps5,
                'opsi_a' => $ps6,
                'opsi_b' => $ps7,
                'kunci_jawaban' => $ps11
            )
        );

        $this->db->insert_batch('soal',$data);
        redirect('guru/index');
    }

view.php

<!DOCTYPE html>
<html lang="en" >

<head>
  <title></title>
</head>

<body>
  
<?php
$i=1;
while ($i<=3){
foreach($tampilan as $soal){
    ?>
    <form action="<?php echo base_url()?>guru/aksi_soal3" method="post">
    
        <?php
    echo "  
            <input type='hidden' name='kode_soal' value='$soal->kode_soal''>
            <textarea placeholder='soal' name='soal'></textarea>

            <input type='text' name='opsi_a' placeholder='jawaban a'>
            <input type='text' name='opsi_b' placeholder='jawaban b'>
            <input type='text' name='kunci_jawaban' placeholder='Kunci jawaban' >
            </div>
            </div>
     ";
    ?>

        <?php
    $i=$i+1;
}}
?>   

<button type="submit" class="btn">Selesai</button>
</form>
</html>
2

There are 2 best solutions below

3
Madhusudan On BEST ANSWER

You need to do something like below,

Controller Part :-

    public function aksi_soal3(){
            $ps5 = $this->input->post('soal');
            $ps6 = $this->input->post('opsi_a');
            $ps7 = $this->input->post('opsi_b');
            $ps11 = $this->input->post('kunci_jawaban');
         $data = array();
        foreach($ps5 as $key=>$value) {
            $data[]  = array(
                    'soal' => $value,
                    'opsi_a' => $ps6[$key],
                    'opsi_b' => $ps7[$key],
                    'kunci_jawaban' => $ps11[$key]
                );
        }
            $this->db->insert_batch('soal',$data);
            redirect('guru/index');
        }

View Part :-

    <form action="<?php echo base_url()?>guru/aksi_soal3" method="post">
    
    <input type='hidden' name='kode_soal[]' value='$soal->kode_soal''>
    <textarea placeholder='soal' name='soal[]'></textarea>
    <input type='text' name='opsi_a[]' placeholder='jawaban a'>
    <input type='text' name='opsi_b[]' placeholder='jawaban b'>
    <input type='text' name='kunci_jawaban[]' placeholder='Kunci jawaban' >
    
    <button type="submit" class="btn">Selesai</button>
    </form>
0
Rizki Masjahri On
<div class="after-add-more">
<input type="text"name="StudentID[]" value="">
<div class="col-md-5">
<div class="form-group remove">
</div>
</div>
</div>
 <div class="form-group">
<button class="btn btn-success btn-fill add-more">Add More</button>
</div>

jQuery(document).ready(function() {
$("body").on("click",".add-more",function(){
    var html = $(".after-add-more:first").clone();
    $(html).find(".remove").html("<br><a class='btn btn-danger btn-fill remove'>Remove</a>");
  $(".after-add-more").last().after(html);
});
$("body").on("click",".remove",function(){ 
    $(this).parents(".after-add-more").remove();
});
});

you need something like this : http://jsfiddle.net/cudkfraj/

when you click on add more button, the text field populate as many as you click, then you can change the name attribute of your form field to be like that code, with bracket[] so you can save your value as an array