【发布时间】:2014-07-07 10:01:41
【问题描述】:
我使用 Jquery UI 来更新我的表格位置。它运行良好。但现在我想将参数从 ajax 传递给 php 以使用当前表位置更新我的数据库。但我得到 $('#widget').html(data.html);类型错误:数据=空。并且无法将参数从 ajax 传递给 php。在 firebug 中我可以看到参数正在传递,但为什么我不能将它用于我的 php 文件不知道。我什至尝试回显更新语句,但它没有回显。 这是我的代码: js代码:
$("#widget_update").sortable({
update: function() {
var widget = $(this).sortable("serialize") + '&action=updatewidget';
//var element_id1 = ui.item[0].id;
//alert(element_id1);
$.ajax({
type: "POST",
url: "ajax/dashboard.php",
dataType : 'json',
cache: false,
data: {'aktion' : 'show-widget','new_widget':widget},
success: function(data){
$('#widget').html(data.html);
},
error: function(data){
alert('Error');
}
});
}
});
php代码:
if($param['aktion'] == 'show-widget')
{
$page['button'] = array(
1 => array( 'Add Widget','pfeil2r','',"'#'",'','','addwidgetId'),
2 => array( 'Remove Widget','pfeil2r','',"'#'",'','','removewidgetId'),
);
$action = mysql_real_escape_string($_POST['action']);
$updateRecordsArray = $_POST['recordsArray'];
print_r($action);
if ($action == "updatewidget"){
$position = 1;
foreach ($updateRecordsArray as $widget_id) {
echo $sql="Update dashboard_widget_users inner join dashboard_widget on dashboard_widget_users.dsnr_dashboard_widget=dashboard_widget.ID
set dashboard_widget_users.position=".$position."
where dashboard_widget.id=".$widget_id."
and dashboard_widget_users.dsnr_yw_user=10";
$sql_update=mysql_query($sql);
$position = $position + 1;
}
}
$html= '<table width="538" cellspacing="0" cellpadding="0" border="0" >
<tr>
<td>
<table id="widget">
<td>
</td>
</table>
</td>
</tr>
<tr>
<td colspan="2">
'.CreateButton($page['button']).'
</td>
</tr>
</table>';
$return = array(
'status' => 1,
'html' => $html
);
echo json_encode($return);
die();
}
?>
【问题讨论】:
-
$param['aktion']应该是$_POST['aktion'] -
是
$param['aktion']设置自$_POST['aktion'] -
你应该在使用它之前尝试解码JSON吗?
-
@user1835565,当
dataType设置为json时,jQuery会自动解析json -
谢谢,不知道。
标签: javascript php ajax parameter-passing