我试图onclick
Jquery并发布到msg.php
。我的代码有什么问题吗?请让我知道问题所在。谢谢。
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>
function markAsRead(msgID) {
var url = "msg.php";
var ownerid = "7";
var messageid = "msgID";
$.post(url,{messageid: messageid, ownerid: ownerid });
}
</script>
<a onclick="markAsRead(<?php echo $row['id'];?>)">how are you?</a>
`
msg.php
if (isset($_POST['messageid'])) {
$messageid = preg_replace('#[^0-9]#i', '', $_POST['messageid']);
$ownerid = preg_replace('#[^0-9]#i', '', $_POST['ownerid']);
mysqli_query("UPDATE private_messages SET opened='1' WHERE id='$messageid'
LIMIT 1");
}
}
else
echo "There is no post value";
答案 0 :(得分:1)
您应该设置$row['id']
<?php
$row['id'] = 1; // the id is 1 for example
?>
<a onclick="markAsRead(<?php echo $row['id'] ?>)">how are you?</a>
<script>
function markAsRead(msgID) {
var url = "msg.php";
var ownerid = 7; // this should be unquoted
var messageid = msgID; // this should be unquoted
$.post(url, {
messageid: messageid,
ownerid: ownerid
}, function () {
alert('Marked!'); // added this as a callback
}).error(function(){
alert('Failed to mark.'); // as an error callback
});
}
</script>
PHP代码
<?php
if (isset($_POST['messageid']))
{
$messageid = intval($_POST['messageid']); // seems your replacing numbers
$ownerid = intval($_POST['ownerid']); // seems your replacing numbers
mysqli_query("UPDATE private_messages SET opened = 1 WHERE id = $messageid LIMIT 1");
}
else
{
echo "There is no post value";
}
?>