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

时间:2021-12-11 13:41:42

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构造的数组,其中包含多个select的选定值。是否有一种方法可以使用ajax将这个数组发送到php脚本?

6 个解决方案

#1


18  

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-PHP for parsing JSON.

您可以使用XML或JSON返回服务器。您的javascript将必须构造post,在XML的情况下,这将要求您使用javascript创建post。JSON不仅重量轻,而且在javascript中更容易制作。查看JSON- php来解析JSON。

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

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

#2


26  

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 myphpvariable and it will contain the myJavascriptArray values.

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

Is it that ?

是吗?

#3


3  

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 $var that 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,它将自动生成一个名为$var的数组,其中包含foo、bar和baz。我认为您甚至可以在查询字符串的方括号中指定数组索引,它将在该索引中插入值。您可能需要URL对括号进行编码……通常使用这个特性的方法是创建一个名为“var[]”的HTML输入字段,所以只要在浏览器中正常运行即可。PHP文档中有一节是关于数组变量的请求。

#4


1  

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

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

#5


1  

jQuery 1.4 was 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:

这是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

#6


0  

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,但在其他库/ plain old 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}

#1


18  

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-PHP for parsing JSON.

您可以使用XML或JSON返回服务器。您的javascript将必须构造post,在XML的情况下,这将要求您使用javascript创建post。JSON不仅重量轻,而且在javascript中更容易制作。查看JSON- php来解析JSON。

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

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

#2


26  

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 myphpvariable and it will contain the myJavascriptArray values.

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

Is it that ?

是吗?

#3


3  

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 $var that 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,它将自动生成一个名为$var的数组,其中包含foo、bar和baz。我认为您甚至可以在查询字符串的方括号中指定数组索引,它将在该索引中插入值。您可能需要URL对括号进行编码……通常使用这个特性的方法是创建一个名为“var[]”的HTML输入字段,所以只要在浏览器中正常运行即可。PHP文档中有一节是关于数组变量的请求。

#4


1  

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

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

#5


1  

jQuery 1.4 was 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:

这是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

#6


0  

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,但在其他库/ plain old 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}