我有这个 jQuery 代码,它只是假设在数据库中找到记录时显示新消息的警报。
index.html(jQuery 代码)
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script>
$(document).ready(function(){
var count = 0;
setInterval(function() {
$.post("messagecheck.php", { countOld: count },
function(data){
if(data == 0) {
alert("No New Messages");
return;
} else {
count = data; // This will change the count for each run, you could store this in div with .data() ...
alert("New Message!");
return;
}
});
}, 1000);
});
</script>
消息检查.php
<?php
if($uid == 0) {
die(); // not logged in
} else {
$sql = 'SELECT messagecount FROM Users WHERE uid = $uid AND messagecount >= 1';
$result = mysql_query($sql);
if(!$result) {
// Kill SQL and return error
} else {
// We will be sending an Old Count via POST
$numRows = mysql_num_rows($result);
echo $numRows;
if( $numRows == $_POST['countOld']) {
// No change
echo 'No change: 0';
} else
echo $numRows;
}
}
?>
即使 $uid 为 0(用户未登录),新消息的警报!显示。
我能做些什么来解决这个问题?
谢谢。