我正在尝试使用AJAX将4个js数组发送到php页面,一旦我想要它们在4个php数组中用于php脚本。
使用$.type(var)
我已经确认我的js数组实际上是数组。
使用以下命令发送一个数组:
$.ajax({
type: "POST",
cache: false,
url: "test.php",
data : arrayA: arrayA,
success: function(res) {
console.log (res)
}
});
但以下失败:
var data = { arrayA: arrayA, arrayB: arrayB, arrayC: arrayC, arrayD: arrayD }
$.ajax({
type: "POST",
cache: false,
url: "test.php",
data : data,
success: function(res) {
console.log (res)
}
});
我的php页面是:
$arrayA = $_REQUEST['arrayA'];
$arrayB = $_REQUEST['arrayB'];
$arrayC = $_REQUEST['arrayC'];
$arrayD = $_REQUEST['arrayD'];
我确定我错过了显而易见的事,请有人建议。
由于
答案 0 :(得分:0)
试试这个,这个方法应该有效:
// Create the arrays you want
var arrayA = ["cat","dog"];
var arrayB = ["lamp","rock"];
// Combine the arrays into one
var array = [arrayA, arrayB];
// Send them via ajax
$.ajax({
type: "POST",
cache: false,
url: "test.php",
data : array: array,
success: function(res) {
console.log (res)
}
});
PHP方面:
<?php
// (use print_r($_POST) to see what comes through and how it is setup in the POST array, that way you can get the keys and names and build it out how you wish
foreach($_POST['array'] as $array){
// This will give each array, you could also get the array keys and use those as names if you wish
}
?>