当用户单击收件箱中的通知时,我正在尝试更新读取的内容。我在互联网上看到使用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");
}
})
});
我希望有人能看到问题。
添加的图片
答案 0 :(得分:1)
$(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");
}
})
});
答案 1 :(得分:0)
实际上,您不是在检查脚本的响应是什么,而只是发出警报(“记录已成功更新”);
您应该这样做:
$.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;
}
?>
答案 2 :(得分:0)
问题很可能出在您的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;
}
}
?>
编码愉快!