0

I have a dynamically generated form that allows users to enter new data and edit existing data. When the form is submitted, it collates the input values and groups them according to whether they are new or not, the former being denoted by class="new-entry".

So the function generates two arrays: updateData and insertData. Both arrays are of similar formats:

[
    0: {
        'id'    = 1,
        'value' = foo
    },
    1: {
        'id'    = 1,
        'value' = 'bar'
    },
    etc...
]

I am combining them into a new array object to send via ajax to the controller:

var postData = {
    'update_data': updateData,
    'insert_data': insertData
};

Then in the ajax call:

$.post(url, postData, function() { // some code });

However, in the controller, doing print_r($this->input->post()) or print_r($_POST) as a test only returns Array(). Even $this->input->post('update_data') returns nothing.

How can I retrieve these arrays in the controller?

6
  • is updateData a JSON string? Otherwise I suggest JSON.stringify(updateData). Same goes for insertdata of course. Commented Oct 29, 2013 at 9:35
  • I just tried your code on my localhost and it works fine. but what I did and I don't know if your doing the same is that I used $('.form').serializeArray() you might need it! Commented Oct 29, 2013 at 9:50
  • @Johan I tried using JSON.stringify() on updateData and insertData, but the controller returned nothing at all, not even an empty array. Commented Oct 29, 2013 at 11:11
  • @mamdouhalramadan I would use serializeArray(), but I need to populate the arrays manually using data attributes tied to the inputs. Commented Oct 29, 2013 at 11:12
  • can you give an example?! Commented Oct 29, 2013 at 11:19

1 Answer 1

1

Its not an issue with Codeigniter. Convert the array to proper JSON object (stringify) before you send.

Use

var postData = {
    'update_data': JSON.stringify(updateData),
    'insert_data': JSON.stringify(insertData)
};

$.post(url, postData, function() { // some code });

and in your controller, you can get by

$update_data=json_decode($this->input->post('update_data'));
$insert_data=json_decode($this->input->post('insert_data'));
Sign up to request clarification or add additional context in comments.

1 Comment

I tried this before, but I couldn't get it to return anything.

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.