我正在使用jQuery可排序插件并尝试使用AJAX更新我的数据库,但我无法传递隐藏的输入字段值并获得" Undefined Index"错误信息。
这是我的剧本:
$( "#sortable" ).sortable({
update: function( event, ui ) {
var postData = $(this).sortable('serialize');
$.ajax({
data: postData,
type: 'POST',
url: 'save.php'
});
}
});
这是我的表格:
<div id="sortable">
<div id="city">
<?php echo $city_name ?>
<input type="checkbox" name="cityStatus[<?php echo $city_ID ?>]" value="1" <?php echo $checked ?>>
<input type="hidden" name="cityID[<?php echo $row->city_ID ?>]" value="<?php echo $city_ID ?>">
<input type="hidden" name="cityOrder[<?php echo $row->city_ID ?>]" value="<?php echo $city_order ?>">
</div>
</div>
我的save.php文件:
foreach($_POST['cityStatus'] as $key => $value) {
$cityID = $_POST['cityID'][$key];
$cityStatus = $_POST['cityStatus'][$key];
$cityOrder = $_POST['cityOrder'][$key];
$updateCities = $db->execute(
"UPDATE cities
SET city_status=?, city_order=?
WHERE city_ID=$cityID",
array($cityStatus, $cityOrder));
}
答案 0 :(得分:0)
你没有告诉JS如何发送你的POST参数。将您的JS更改为:
data: { 'postData':postData }
这基本上是指key = value形式的"postData="
+ postData
。它告诉javascript和PHP你希望变量postData
链接到键“postData”,然后你就可以用$_POST['postData']