我使用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