如何将我的ajax数据导入php数组?

时间:2012-07-16 18:11:01

标签: php jquery ajax

我在JS脚本中有以下数据:

$("#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

我需要将其发送到php文件以更新数据库并通过电子邮件发送更新。我的console.log(arr);当我在firebug中运行它时显示对象,但我无法获得php端的任何信息。这是我的PHP代码:

$data = $_POST['data']; 

print_r($data); die;

此时我只是想验证是否有信息被发送过来。我的print_r($ data);没有回报。 有人能指出我正确的方向吗?

3 个答案:

答案 0 :(得分:1)

使用json_encode()json_decode()方法

答案 1 :(得分:1)

dataType: "json"表示您希望在请求中检索json数据而不是您要发送的内容。

如果要发送要由$_POST['data']使用

检索的json字符串
data: {data: JSON.stringify(arr)},

答案 2 :(得分:0)

使用下一个方式:

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

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

注意:尚未对其进行测试,请务必查找解析错误。