Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用AJAX将数组发送到PHP_Php_Jquery_Arrays_Ajax - Fatal编程技术网

如何使用AJAX将数组发送到PHP

如何使用AJAX将数组发送到PHP,php,jquery,arrays,ajax,Php,Jquery,Arrays,Ajax,我有这个数组 var Age=[$(".AJcountry").find(".focus").html()]; 这是阿贾克斯 $.ajax({ type: "GET", url: '../connect.php', data: "CountS=" + Age, success: function (dataa) { alert(dataa); } }); 但是它只返回最后一个选中的项目。如何使用循环将数组中的所有内容发送到PHP

我有这个数组

var Age=[$(".AJcountry").find(".focus").html()];
这是阿贾克斯

$.ajax({
    type: "GET",
    url: '../connect.php',
    data: "CountS=" + Age,
    success: function (dataa)
    {
        alert(dataa);
    }
});
但是它只返回最后一个选中的项目。如何使用循环将数组中的所有内容发送到PHP

var Age=[];

$(".AJcountry").find(".focus").each(function(){
Age.push($(this).html());
});
在ajax中:

data: {CountS:Age},
在php中,您可以:

echo $_GET['CountS'][0];//get the first

或者循环

如果为
$的
数据
属性提供了对象。ajax
则jQuery将为您序列化它:

data: { CountS: Age }

还要注意的是,当您将
.focus
元素的
html()
值放入一个数组时,该数组始终只包含一个字符串元素,因此在这种情况下,数组本身似乎是完全冗余的。

将数组转换为JSON,如下所示:

var json = JSON.Stringify(Age);
然后通过AJAX发送,就像您正在做的那样:

data: {CountS: Age}
在后端,将JSON转换回如下数组:

$json = json_decode($_GET['CountS']);

此方法在传输数据之前将其转换为非常小的文本字符串。PHP后端然后将其转换回数组。此方法也适用于JavaScript中的大多数对象。您可以阅读更多关于JSON的信息

什么是
console.log(Age)返回?上面写着等10分钟实际上madalin的方法对我有效。我试过你的答案,结果是空白的