Codeigniter loop through post array

孤者浪人 提交于 2021-02-05 09:41:02

问题


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);

回答1:


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);

    }

}


来源:https://stackoverflow.com/questions/16691304/codeigniter-loop-through-post-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!