我使用AJAX將一些數據放在我的數據庫中。我正在使用JSON將數據提交到PHP頁面。我正在使用POST請求。你可以將一個POST變量設置爲一個數組嗎?下面我有充分的AJAX請求,但這裏是我設置的數據的兩個部分作爲陣列的一部分:
"content[]" : testContentArray,
"content_attr[]" : testContentAttributes,
下面是我的全部AJAX(使用jQuery):
$.ajax({
type: "POST",
url: "../includes/create_test_main.ajax.php",
data: {"tags" : $('#testTags').val(),
"title" : $('#testTitle').val(),
"subject" : $('#testSubject').val(),
"description" : $('#testDescription').val(),
"content[]" : testContentArray,
"content_attr[]" : testContentAttributes,
"user_id" : user_id},
success:
function(testId) {//redirect Page
window.location.href = "http://localhost/nvr_forget/public_html/test/" + testId + "/";
}
});
};
如果那就是實現它的方式,我將如何在PHP端處理它?作爲普通數組處理$ _POST變量?
那麼你將如何循環這些數組? 'while($ _ POST ['content']){/ * do something * /}'? – chromedude 2011-05-28 21:08:06
@chromedude你不能使用'while'來循環訪問一個數組(你可以,但是在幾乎所有可能的情況下它都是愚蠢的)。使用['foreach'](http://php.net/manual/en/control-structures.foreach.php)。 – lonesomeday 2011-05-28 21:09:43
jQuery.param基本上完成了我的解決方案,但就像我說的,你只是使用另一個序列化程序..這很好,我想如果你只是瞎搞:P – Halcyon 2011-05-28 21:13:04