我有一个包含可排序行的表。该表是从 mysql 数据库生成的,该数据库有一个名为“displayorder”的字段,用于对表进行排序。当用户拖放行时,我想在用户拖放行时使用 AJAX 将这些更改提交到数据库。
目前,我可以看到 AJAX 调用成功部分的 console.log() 输出,当我在那里输出数据(顺序)时,它看起来很棒,如下所示:
[“order_1=1”、“order_2=2”、“order_4=3”、“order_3=4”]
但根据 Firebug,$_POST 中传递的所有内容都是“未声明的”。 如何从我的indexpage_order.php 文件访问该订单变量?
我有这个 jquery 代码:
<script>
$(document).ready(function() {
var fixHelper = function(e, tr) {
var $originals = tr.children();
var $helper = tr.clone();
$helper.children().each(function(index)
{
$(this).width($originals.eq(index).width())
});
return $helper;
};
var sortable = $("#sort tbody").sortable({
helper: fixHelper,
stop: function(event, ui) {
//create an array with the new order
order = $(this).find('input').map(function(index, obj) {
var input = $(obj);
input.val(index + 1);
return input.attr('id') + '=' + (index + 1);
});
$.ajax({
type: 'POST',
url: 'indexpage_order.php',
data: order,
error: function() {
console.log("Theres an error with AJAX");
},
success: function(order) {
console.log("Saved.");
console.log(order);
}
});
return false;
}
});
});
</script>
indexpage_order.php 包含:
if(isset($_POST) ) {
while ( list($key, $value) = each($_POST) ) {
$id = trim($key,'order_'); //trim off order_
$sqlCommand =
"UPDATE indexpage
SET displayorder = '".$value."'
WHERE id = '".$id."'";
$query = mysqli_query($myConnection,$sqlCommand) or die (mysqli_error($myConnection));
$row = mysqli_fetch_array($query);
}
}
最佳答案
您可以简单地重写为 POST 生成数据的 js 代码。
order = {}
$(this).find('input').map(function(index, obj)
{
return order[this.id] = index;
}
)
其余的应该可以在 PHP 中工作。
关于php - Ajax 调用不传递任何 POST 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12625227/