[英]Get array through an jQuery AJAX call doesn't work
I have the following code: 我有以下代码:
var data = $(this).sortable('serialize');
$.ajax({
data: {order: data, actionFor: 'main_articles'},
type: 'POST',
url: 'updateDisplayOrder.php',
success: function (msg) { //re-number the rows from 1 to n
//code goes here
},
error: function () {
alert("An error occurred");
});
And the PHP: 和PHP:
require_once('../lib/common/db_connect.php');
$ids = array();
$actionFor = $_POST['actionFor'];
foreach ($_POST['order'] as $value) //error here {
//more code goes here
}
The problem is that I get this error on the foreach line: 问题是我在foreach行上收到此错误:
Warning: Invalid argument supplied for foreach()
警告:为foreach()提供了无效的参数
I noticed that if I change this line: 我注意到,如果我更改此行:
data: {order: data, actionFor: 'main_articles'},
To 至
data:data
And in the PHP : 在PHP中:
foreach ($_POST['order'] as $value) //error here {
To 至
foreach ($_POST['item'] as $value)
It Works great. 效果很好。 Why?
为什么? How can I fox it?
我该如何欺骗它? thanks!
谢谢!
$_POST['order']
contains a string, not an array. $_POST['order']
包含字符串,而不是数组。 You need to parse your string into an array first. 您需要首先将字符串解析为数组。 Try this:
尝试这个:
$order = array();
$stg1 = explode("&", $_POST['order']);
$i = 0;
foreach($stg1 as $keyval) {
list($key, $val) = explode("=", $keyval);
$order[$i++] = $val;
}
Then you can do your foreach($order as $value)
as you wish. 然后,您可以根据需要执行
foreach($order as $value)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.