我在php中执行一个页面,如果输入任何新记录,它将通过新记录计数通知用户屏幕。以下是我为此做的代码,但它不能正常工作。你可以告诉我,我做错了什么......
alert.php
<?php
require("config.php");
$result = mysql_query("SELECT * FROM marketing_tend_corr");
$res = mysql_num_rows($result);
echo $res;
?>
index.php
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<?php
define('BASEPATH', true);
require("config.php");
?>
<script>
var count_cases = -1;
setInterval(function(){
$.ajax({
type : "POST",
url : "alert.php",
success : function(response){
if (count_cases != -1 && count_cases != response) echo $count_cases);
count_cases = response;
}
});
},1000);
</script>
答案 0 :(得分:0)
以下代码行无法在Javascript中使用:
if (count_cases != -1 && count_cases != response) echo $count_cases);
这行代码包含php代码(echo $ count_cases),它是服务器端代码。
我已经改变了一些代码并通过返回一个随机值来替换记录数。
// alert.php
<?php
echo rand(1, 1000000);
//的index.php
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
setInterval(function(){
$.ajax({
type : "POST",
url : "alert.php",
success : function(response){
$("body").html(response);
}
});
},1000);
</script>
</head>
<body>
</body>
</html>
您可以在浏览器中查看index.php文件,以查看返回的随机数。在你的情况下,这个随机数应该是你的'mysql_num_rows'函数的结果。