WhiteProfileSpecialist
WhiteProfileSpecialist

Reputation: 119

Codeigniter loop through post array

So i have this form:

    <form id="stepform" action="#" method="post">
        <fieldset>
        <legend>Step #1</legend>
        <label>Title</label>
        <input type="hidden" name="csrf_modo" value="b94961394f8e6f7efaa4e37ca9007822">
        <input type="text" name="field[]" class="input-xlarge">
        <label>Body</label>
        <textarea class="input-xlarge" name="field[]"></textarea>
        </fieldset>
    </form>

When user clicks a button jquery dynamically appends another two exactly same fields:

 count = 2;
$("#addstep").click(function(){


    $('#stepform').append('<legend>Step #' + (count++) + '</legend>');
    $('#stepform').append('<label>Title</label><input type="text" name="field[]" class="input-xlarge">');
    $('#stepform').append('<label>Body</label><textarea class="input-xlarge" name="field[]"></textarea>');
    $('#addstep').scrollintoview();
    return false;

});

As you can see one step has 2 fields, when user clicks on the button step increments and adds another 2 fields to that step and so on... After that i send data to the controller via ajax request. Now i'm stuck on the actual query which should insert new row for every step. How could i accomplish this?

Btw i'm using codeigniter and it's bind queries:

$this->db->query($sql, $data);

Upvotes: 0

Views: 3775

Answers (1)

thpl
thpl

Reputation: 5910

Update

I corrected the handling of the difference between textareas and input fields. Sidenote: The whole Controller logic belongs into a model. I just put it into the Controller here for simplification reasons.

HTML

<form id="stepform" action="#" method="post">
    <fieldset>
    <legend>Step #1</legend>
    <label>Title</label>
    <input type="hidden" name="csrf_modo" value="b94961394f8e6f7efaa4e37ca9007822">
    <input type="text" name="field[input][]" class="input-xlarge">
    <label>Body</label>
    <textarea class="input-xlarge" name="field[textarea][]"></textarea>
    </fieldset>
</form>

JS

count = 2;
$("#addstep").click(function(){


    $('#stepform').append('<legend>Step #' + (count++) + '</legend>');
    $('#stepform').append('<label>Title</label><input type="text" name="field[input][]" class="input-xlarge">');
    $('#stepform').append('<label>Body</label><textarea class="input-xlarge" name="field[textarea][]"></textarea>');
    $('#addstep').scrollintoview();
    return false;

});

PHP

class SomeController extends MY_Controller{

    public function process_request()
    {
        $insert_data = array();
        $field_data = $this->input->post('field');

        for($i = 0; $i < count($field_data['input']); $i++)
        {
            $insert_data[] = array(
                'db_col_name_input' => $field_data['input'][$i],
                'db_col_name_textarea' => $field_data['textarea'][$i]
            );
        }

        $this->db->insert_batch('my_table', $insert_data);

    }

}

Old Answer:

Since you're appending square brackets to your input fields name you will get an array with the values of all fields that are having this name. So you can go trough them with a foreach loop and store all the values in an array and use CodeIgniters insert_batch() Method to insert multiple data at the same time.

class SomeController extends MY_Controller{

    public function process_request()
    {
        $insert_data = array();

        foreach($this->input->post('field') AS $field)
        {
            $insert_data[] = array(
                'db_col_name' => $field
            )
        }

        $this->db->insert_batch('my_table', $insert_data);

    }

}

Upvotes: 4

Related Questions