1

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?

1
  • Use the appropriate tools. In this case, Firebug, fiddler2, Developer Tools, etc .. then monitor the network requests. Then, if there is something sent, simplify the question only the relevant PHP stuff that deals with the title and post what is sent .. Commented Jul 16, 2012 at 18:12

3 Answers 3

1

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)},
Sign up to request clarification or add additional context in comments.

2 Comments

If I use your suggestion, do I need to then use json_decode() in the php file? ie $data = json_decode($_POST['data']?
@Jim Yes, I don't think there is any method that wouldn't require it.
1

Use the json_encode() and json_decode() methods

Comments

0

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.

Comments

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.