2015-10-20 45 views
3

我有兩個不同的數組,即main_name []和sub_name []。 我必須使用AJAX將這些值傳遞到下一頁,以便插入到DB中。請您幫我解決,我怎樣才能將數組作爲AJAX中的數據傳遞。 HTML將文本框數組傳遞給ajax json

<input type="text" name = "main_name[]" value = "" > 
<input type="text" name = "sub_name[]" value = "" > 

JS

$.ajax({ 
type: "POST", 
dataType: "json", 
url: "response.php", //Relative or absolute path to response.php file 
data: data, 
success: function(data) { 
$(".the-return").html 
); 
alert("Form submitted successfully.\nReturned json: " + data["json"]); 
} 
}); 
return false; 
}); 
}); 
+0

嘗試'數據:$( '形式')序列化()''中ajax' – Tushar

+0

@Tushar你能建議我在代碼格式請 – user1894647

回答

1

試試這個它會得到你的表格中的所有值(數據)變量,通過Ajax JSON格式發送。

var data = $('form').serialize(); 
$.ajax({ 
type: "POST", 
dataType: "json", 
url: "response.php", //Relative or absolute path to response.php file 
data: data, 
success: function(data) { 
$(".the-return").html 
); 
alert("Form submitted successfully.\nReturned json: " + data["json"]); 
} 
}); 
return false; 
}); 
}); 
2

$('form').serialize()將所有表單元素連同值一起序列化,並與ajax查詢一起發送。

$.ajax({ 
    type: "POST", 
    dataType: "json", 
    url: "response.php", //Relative or absolute path to response.php file 
    data: $('#form_id').serialize(), 
    success: function(data) { 
     $(".the-return").html 
    ); 
    alert("Form submitted successfully.\nReturned json: " + data["json"]); 
    } 
    }); 
    return false; 
    }); 
    }); 
+0

如何檢索response.php中的值 – user1894647

+0

您可以使用'$ _REQUEST'。在response.php –

2

你可以簡單地使用serializeArray

var data = $(form).serializeArray(); 

$.ajax({ 
    type: "POST", 
    dataType: "json", 
    url: "response.php", //Relative or absolute path to response.php file 
    data: data, 
    success: function (data) { 
     $(".the-return").html(); 
    } 
}); 
+0

上嘗試將'var_dump'作爲echo'print_r($ _ REQUEST,true)'回調(警告)對象 – user1894647