Jim
Jim

Reputation: 1315

how do I get my ajax data into a php array?

I have the following data in a JS script:

$("#holdSave").live('click', function () {
    var arr = {};
    var cnt = 0;

    $('#holdTable tbody tr').each(function () {
        arr[cnt] = {
            buyer: $(this).find('#tableBuyer').html(),
            sku: $(this).find('#tableSku').html(),
            isbn: $(this).find('#tableISBN').html(),
            cost: $(this).find('#tableCost').val(),
            csmt: $(this).find('#tableConsignment').val(),
            hold: $(this).find('#tableHold').val()
        };
        cnt++;
    }); //end of holdtable tbody function
    console.log(arr);
    $.ajax({
        type: "POST",
        url: "holdSave.php",
        dataType: "json",
        data: {
            data: arr
        },

        success: function (data) {

        } // end of success function

    }); // end of ajax call

}); // end of holdSave event

I need to send this to a php file for updating the db and emailing that the update was done. My console.log(arr); shows the objects when I run it in firebug, but I can't get any of the information on the php side. Here is what I have for php code:

$data = $_POST['data']; 

print_r($data); die;

At this point I am just trying to verify that there is info being sent over. My print_r($data); returns nothing. Can anyone point me in the right direction please?

Upvotes: 1

Views: 91

Answers (3)

Musa
Musa

Reputation: 97672

dataType: "json" means you are expecting to retrieve json data in your request not what you are sending.

If you want to send a json string to be retrieved by $_POST['data'] use

data: {data: JSON.stringify(arr)},

Upvotes: 1

Novak
Novak

Reputation: 2768

Use the next way:

data = {key1: 'value1', key2: 'value2'};

$.post('holdSave.php', data, function(response) {
    alert(response);
});

Note: haven't tested it, make sure to look for parse errors.

Upvotes: 0

M. Ahmad Zafar
M. Ahmad Zafar

Reputation: 4939

Use the json_encode() and json_decode() methods

Upvotes: 1

Related Questions