当用户点击收件箱中的通知时,我正在尝试更新已解决的问题。我在互联网上看到它应该是非常容易的AJAX和jquery,但我得到一个奇怪的问题。点击AJAX后成功响应,但是当我查看我的数据库时,它没有更新。然后我尝试手工完成并在链接中为它提供了正确的值。之后我得到了响应成功,数据库也成功更新了。所以问题必须是AJAX,但我不知道我做错了什么。
read.msg.php
include("db-conct.php");
$AID = $_GET['aid'];
$msgID = $_GET['msgid'];
$update = "UPDATE notify SET readed = 1 WHERE acc_ID = $AID AND ID = $msgID";
if ($conn->query($update) == TRUE) {
echo "Succes!";
}else{
echo "Error: " . $conn->error;
}
?>
AJAX脚本:
$(document).on("click","#notify-item",function(){
var aid = "<?php echo $aid; ?>";
var msgid = "<?php echo $notifID; ?>";
$.ajax({
type:"POST", //GET - update query should be POST
url: "read.msg.php", //your php end point
data: 'aid=' + aid + '&msgid=' + msgid,
success: function(data){ //if success
alert("Record successfully updated");
}
})
});
我希望有人能看到问题所在。
添加图片:
实际上你没有检查脚本的响应是什么,只是警告(“记录成功更新”);
你应该做这个:
$.ajax({
type:"POST",
url: "read.msg.php",
data: {aid:aid ,msgid:msgid},
success: function(data){
if(data=="success"){
alert("Record successfully updated");
}
else{
alert("Some Error");
}
}
});
php脚本:
<?php
include("db-conct.php");
@extract($_POST);
$aid = $_POST['aid'];
$msgid = $_POST['msgid'];
$update = "UPDATE notify SET readed = 1 WHERE acc_ID = '$aid' AND ID = $'msgid'";
if ($conn->query($update) == TRUE) {
echo "success";
}else{
echo "Error: " . $conn->error;
}
?>
$(document).on("click","#notify-item",function(){
var aid = "<?php echo $aid; ?>";
var msgid = "<?php echo $notifID; ?>";
$.ajax({
type:"POST", //GET - update query should be POST
url: "read.msg.php", //your php end point
data: {'aid':aid ,'msgid':msgid}, // this is the data format
success: function(data){ //if success
alert("Record successfully updated");
}
})
});
问题最可能出在你的php处理代码中。
在更新数据库之前,您必须确保数据不为空。
使用isset
在POST值上包装代码,并尝试回显它以进行测试。
如果它是空的,则表示在发布之前提供数据的javascript存在问题。
<?php
if (isset($_POST['aid']) && isset($_POST['msgid'])) {
//echo $_POST['aid'].' / '.$_POST['msgid'];
include("db-conct.php");
$aid = $_POST['aid'];
$msgid = $_POST['msgid'];
$update = "UPDATE notify SET readed = 1 WHERE acc_ID = '$aid' AND ID = $'msgid'";
if ($conn->query($update) == TRUE) {
echo "success";
}else{
echo "Error: " . $conn->error;
}
}
?>
快乐的编码!