php 如何通过ajax向php发送数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/299610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:17:25  来源:igfitidea点击:

How can I send an array to php through ajax?

phpajaxmultiple-select

提问by Lucia

I want to send an array constructed in javascript with the selected values of a multiple select. Is there a way to send this array to a php script using ajax?

我想发送一个用 javascript 构造的数组,其中包含多个选择的选定值。有没有办法使用ajax将此数组发送到php脚本?

采纳答案by csexton

You can post back to your server with XML or JSON. Your javascript will have to construct the post, which in the case of XML would require you to create it in javascript. JSON is not only lighterweight but easier to make in javascript. Check out JSON-PHPfor parsing JSON.

您可以使用 XML 或 JSON 回发到您的服务器。您的 javascript 将必须构建帖子,在 XML 的情况下,这需要您在 javascript 中创建它。JSON 不仅更轻量级,而且在 javascript 中更容易制作。查看JSON-PHP来解析 JSON。

You might want to take a look at Creating JSON Data in PHP

您可能想看看在 PHP 中创建 JSON 数据

回答by Dragouf

You might do that with $.post method of jQuery (for example) :

您可以使用 jQuery 的 $.post 方法来做到这一点(例如):

var myJavascriptArray = new Array('jj', 'kk', 'oo');

$.post('urltocallinajax', {'myphpvariable[]': myJavascriptArray }, function(data){
   // do something with received data!
});

Php will receive an array which will be name myphpvariableand it will contain the myJavascriptArray values.

Php 将接收一个名为myphpvariable的数组,它将包含 myJavascriptArray 值。

Is it that ?

是那个吗 ?

回答by rmeador

IIRC, if PHP sees a query string that looks like http://blah.com/test.php?var[]=foo&var[]=bar&var[]=baz, it will automatically make an array called $varthat contains foo, bar and baz. I think you can even specify the array index in the square brackets of the query string and it will stick the value in that index. You may need to URL encode the brackets... The usual way this feature is used is in creating an HTML input field with the name "var[]", so just do whatever the browser normally does there. There's a section in the PHP documentation on array variables through the request.

IIRC,如果 PHP 看到一个类似于 的查询字符串http://blah.com/test.php?var[]=foo&var[]=bar&var[]=baz,它会自动创建一个名为$varfoo、bar 和 baz的数组。我认为您甚至可以在查询字符串的方括号中指定数组索引,它会将值保留在该索引中。您可能需要对括号进行 URL 编码...使用此功能的通常方式是创建一个名为“var[]”的 HTML 输入字段,因此只需执行浏览器通常在此处执行的操作即可。PHP 文档中有一节介绍了请求中的数组变量。

回答by charlesbridge

You may be looking for a way to Serialize(jQuery version) the data.

您可能正在寻找一种方法来序列化(jQuery 版本)数据。

回答by kishanio

jQuery 1.4was updated to use the PHP syntax for sending arrays. You can switch it into the old style by using:

jQuery 1.4已更新为使用 PHP 语法发送数组。您可以使用以下命令将其切换为旧样式:

here is the synatax:

这是语法:

jQuery.ajaxSetting.traditional = true;

here is the example

这是例子

$.ajax({    
 traditional: true,
 type: "post",
 url: myURL,
 dataType: "text", 
 data: dataToSend, //this will be an array eg. 
 success: function(request) {
  $('#results').html(request);
 }  // End success
 }); // End ajax method

回答by kishanio

You can create an array and send it, as Meador recommended: (following code is Mootooled, but similar in other libraries / plain old JS)

您可以按照 Meador 的建议创建一个数组并发送它:(以下代码是 Mootooled,但在其他库/普通旧 JS 中类似)

myArray.each(function(item, index)  myObject.set('arrayItems['+index+']', item);
myAjax.send(myObject.toQueryString());

That will send to php an array called arrayItems, which can be accessed through $_POST['arrayItems']

这将向 php 发送一个名为 arrayItems 的数组,该数组可以通过 $_POST['arrayItems'] 访问

echo $_POST['arrayItems'] ; 

will echo something like: array=>{[0]=>'first thing', [1]=> second thing}

会回显类似: array=>{[0]=>'first thing', [1]=> second thing}