0

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);
3
  • 3
    what I do not really understand is why you're giving your input field the same name as your textarea. You can't reference whether your input belongs to the textarea or to the input field so easy later during processing the data. Commented May 22, 2013 at 12:25
  • @ThomasDavidPlat Well for no apparent reason!I will change field name to something else. Thanks! Commented May 22, 2013 at 12:33
  • Take a look on my updated answer. I also corrected this issue Commented May 22, 2013 at 12:40

1 Answer 1

4

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

    }

}
Sign up to request clarification or add additional context in comments.

3 Comments

he needs to split textarea and text input, i think they will insert in same row, so you need to rewrite all of his codes
@rcpayan yeah I already noticed that (look at my comment on the question)
Np :) Glad I could be a help

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.