I am trying to pass an array that looks something like this below using a $.ajax() request.

[
    [
        {
            name: "Bob",
            age: "22"
        },
        {
            name: "Sam",
            age: "28"
        },
        {
            name: "Tom",
            age: "26"
        },
    ],
    [
        batch: "101",
        status: "live"
    ]
]

As you can see there is two arrays with one that is an object.

My question is how do I pass this to codeigniter and read the information.

Currently I'm passing it to Codeigniter by doing something like this.

    var submit = $.ajax({
        url:$('#hiddenurl').val() + 'index.php?/home/checkout/',
        type: 'POST',
        data: {checkoutInformation: checkout_data_arr},
        success: function(response, status, xhr) {
          //do code here
        }
    });

and my Codeigniter controller function looks something like this.

function checkout(){
    //send back Item details in JSON.
    $checkoutInformation = $this -> input -> post('checkoutInformation');

    print_r($checkoutInformation);

}

Currently this is the response from that function

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [name] => Bob
                    [age] => 22
                )
            [1] => Array
                (
                    [name] => Sam
                    [age] => 28
                )

            [2] => Array
                (
                    [name] => Tom
                    [age] => 26
                )

        )

)

As you can see, nothing to do with the second array that's in there. How do I parse thru the data with php to work with the data?

Can anyone point out to me what I am doing wrong?

Thank you.

share

I guess you need to parse the json by client side ?

 var submit = $.ajax({
        url:$('#hiddenurl').val() + 'index.php?/home/checkout/',
        type: 'POST',
        data: {checkoutInformation: checkout_data_arr},
        success: function(response, status, xhr) {
            var obj = jQuery.parseJSON( response );
            //echo obj[0][0].name;
// or walking through loop
$.each(obj[0], function( index, value ) {
  alert( index + ": " + value );
});

        }
    });

Parsing on server side:

function checkout(){
    //send back Item details in JSON.
    $checkoutInformation = $this -> input -> post('checkoutInformation');

    foreach($checkoutInformation[0] as $k => $v) {
     echo $k . ":" . $v['name'] . '-' . $v['age'] . "<br>";
   }

}
share
    
No I want to parse it on the PHP side to do server side work with the data. – BaconJuice Dec 10 '14 at 19:16
    
Take a look now – CarcaBot Dec 10 '14 at 19:18
    
json_decode() expects parameter 1 to be string, array given – BaconJuice Dec 10 '14 at 19:21
    
ok, i've corrected it, i thought $checkoutInformation is json but it was array. – CarcaBot Dec 10 '14 at 19:22
    
The response looks something like this now. 0:Array – BaconJuice Dec 10 '14 at 19:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.