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>
9
  • I can't understand bro,u want to get an array in the controller? could u explain more what u expect to see? Commented Nov 1, 2018 at 5: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[]' Commented Nov 1, 2018 at 5: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 Commented Nov 1, 2018 at 5: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 Commented Nov 1, 2018 at 5:59
  • You're actually creating multiple forms. Is that intentional? Commented Nov 1, 2018 at 5:59

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

3 Comments

This won't work since the OP is creating multiple forms as well.
you need to put a form outside of a loop.
okay it works now i can make different value with this, thanks a lot. I've been trying to crack this out for few days XD and also i have put the form outside of the loop
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.