我想使用jQuery AJAX將JSON發送到PHP文件,基本上我想要做的是獲取一組子元素的值和id,然後將它們分配給JSON對象,然後通過ajax將該對象發送給PHP文件,該文件然後將其處理並將其輸入到數據庫中。使用jQuery通過AJAX發送JSON到PHP
這裏是我的代碼,
的Javascript/jQuery的:
function test(){
var selects = $('#systems_wrapper').find('.dropDowns');
var newArray = new Array();
selects.each(function(){
var id = $(this).attr('id');
var val = $(this).val();
var o = { 'id': id, 'value': val };
newArray.push(o);
});
$.ajax({
type: "POST",
url: "qwer.php",
dataType: 'json',
data: { json: newArray }
});
}
PHP:
<?php
$json = $_POST['json'];
$person = json_decode($json);
$file = fopen('test.txt','w+');
fwrite($file, $person);
fclose($file);
echo 'success?';
?>
它創建的文件,但它是完全空白的,任何想法可能是?
Thanx提前!
在您的''調試'print_r($ _ POST);'檢查JS是否正常。在你的'$ .ajax()' – Zlatev 2010-09-08 12:39:17
中你不需要'dataType:'json''我收到一個數組,返回數組 ( [0] => stdClass Object ( [id] => mail_1 [值] => 150升眼鏡蛇(G2) ) [1] => stdClass的對象 ( [ID] => mail_2 [值] => CPC1518 ) ),我在努力處理它,但我可以像這樣訪問數組,例如$ array [0] ['id']? – Odyss3us 2010-09-08 12:57:40
當我print_r($ _ POST),雖然我得到這個...陣列 ( [json] => [{「id」:「mail_1」,「value」:「150 Liter Cobra(G1)」},{「id」:「mail_2」,「value」:「200 Liter Cobra (G1)「}] ) – Odyss3us 2010-09-08 13:01:13