2

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>
KUMAR
  • 1,993
  • 2
  • 9
  • 26
Happy Dog
  • 87
  • 1
  • 8
  • I can't understand bro,u want to get an array in the controller? could u explain more what u expect to see? – Ali Nov 01 '18 at 05:48
  • You have used the same name in the input it will overwrite your previous value, you need to add the name as an array like name='kode_soal[]' – Madhusudan Nov 01 '18 at 05:52
  • i wanted to make multiple input with different value for example data 1: soal:tes1 opsi_a:tes1 opsi_b:tes1 kunci_jawaban:tes1 data 2: soal:tes2 opsi_a:tes2 opsi_b:tes2 kunci_jawaban:tes2 data 3: soal:tes3 opsi_a:tes3 opsi_b:tes3 kunci_jawaban:tes3 – Happy Dog Nov 01 '18 at 05:57
  • the problem right now is just only taking data 3 only and make it become three row with only tes3 but not involved data 1 and data 2 in the database so all the data row is tes3 – Happy Dog Nov 01 '18 at 05:59
  • You're actually creating multiple forms. Is that intentional? – M. Eriksson Nov 01 '18 at 05:59
  • @MagnusEriksson yes that is so that i can make different value for that – Happy Dog Nov 01 '18 at 06:00
  • But when you post a form, only that data will be sent so your controller will only get 5 values. Then the array you're creating in the controller doesn't make sense. Can you please update your question with a proper explanation of what you're trying to do and what the expected result is? It's very unclear. – M. Eriksson Nov 01 '18 at 06:05
  • @MagnusEriksson so you mean i need to make different form? for each different value that i want to get and is like i need 5 form for 5 different value? is that what you mean? – Happy Dog Nov 01 '18 at 06:25
  • No, that's what you're currently creating. If you want to post all data at the same time, you need to create your loop _inside_ the form tags instead of having the form tags inside the loop. You should only have _one_ form. You also need to add `[]` after you names (as others have suggested) to get the input values as arrays in your controller. – M. Eriksson Nov 01 '18 at 06:29

2 Answers2

2

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>
Robert Christopher
  • 4,940
  • 1
  • 20
  • 21
Madhusudan
  • 401
  • 3
  • 12
2
<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