2012-09-29 25 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」 –