2012-07-16 77 views
1

我有一个JS脚本以下数据:如何将我的ajax数据导入到php数组中?

$("#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);当我在萤火虫中运行它时显示了这些对象,但我无法获得任何关于php的信息。这里是我的php代码:

$data = $_POST['data']; 

print_r($data); die; 

在这一点上,我只是想验证有信息被发送过来。我的print_r($ data);什么也不返回 任何人都可以指向正确的方向吗?

+0

检索使用适当的工具。在这种情况下,Firebug,fiddler2,Developer Tools等,然后监视网络请求。然后,如果*有*什么发送,简化问题*仅*相关的PHP的东西,处理标题和职位*什么*发送。 – 2012-07-16 18:12:53

回答

1

dataType: "json"意味着您期望在您的请求中检索json数据,而不是您要发送的数据。

如果您想发送一个JSON字符串由$_POST['data']使用

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

如果我使用你的建议,我需要然后使用json_decode()在PHP文件?即$ data = json_decode($ _ POST ['data']? – Jim 2012-07-16 18:32:44

+0

@Jim是的,我不认为有任何方法不需要它。 – Musa 2012-07-16 18:34:55

1

使用json_encode()json_decode()方法

0

使用下一个方法:

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

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

注:没有测试过,一定要寻找解析错误。

相关问题