2012-09-29 21 views
0

这里是JavaScript代码:Java脚本阵列(jquery的)和JSON

var jsonData = JSON.stringify(testObj); 
      $.ajax({ 
       url: '../php/functions/spesific_field_set.php', 
       type: 'post', 
       data: {fieldObjArray: jsonData, tableName: fieldTableName} 
       }).always(SpesificPropertiesSet); 

和这里是PHP:

$updates = mysql_real_escape_string($_POST['fieldObjArray']); 
$updates = json_decode($updates, true); 
$tableName = mysql_real_escape_string($_POST['tableName']); 

echo $updates; 

什么testObj是对象的阵列,我应该如何将它传递给PHP?以及我应该如何访问php端的这个数组中的数据?

谢谢!!

+0

''var_dump($ updates)''在'json_decode()'看到它看起来像什么后,你就会得到你的答案。 –

+1

顺便说,除非你是直接将整个JSON字符串到你的数据库(可能是一个指示设计不良),不从$ _ POST调用'mysql_real_escape_string()'输入JSON ..的 –

+0

可能重复[如何从Javascript数据传递给PHP,反之亦然?](http://stackoverflow.com/questions/406316/how-to-pass-data-from-javascript-to-php-and-vice-versa) –

回答

2

这是PHP文件。这应该告诉你如何访问$updates这是通过AJAX发送。

$updates = $_POST['fieldObjArray']; 
$updates = json_decode($updates, true); 
$tableName = $_POST['tableName']; 
echo $updates; // this is an array so this would output 'Array' 

foreach ($updates as $key => $value) { 
    echo 'Key: '.$key.' Value: '.$value.'<br />'; // to access this, just use $updates['key'] 
} 

// example 
echo $updates['something']; 
+0

[www.php.net/json_decode](http://www.php.net/json_decode) - >' ** ** assoc命令:为真时,返回的对象将转换成关联arrays.'这是最初在OP的代码 – rationalboss

+0

确实设置为true,对不起,我错过了。尽管如此,它将是一个二维数组,因此1D循环将只输出“Array,Array,Array” –