我一直试图找出为什么我的ajax调用和sql查询无法正常工作的原因。当我查看脚本时,找不到任何错误,我的请求页面如下所示:
echo '<html><body>
<div class="first-row" bordercolor="yellow">
<div id="countdown"></div>
<script>
function UpdateRecord()
{
var userid = '.$userid.';
$.ajax({
type:"POST",
url:"spotlightcount.php",
data:{ userid: userid },
success:function () {
alert("Ok!");
}
});
}
</script>
<script>
var timeleft = 10;
var downloadTimer = setInterval(function(){
document.getElementById("countdown").innerHTML = timeleft + " seconds remaining";
timeleft -= 1;
if(timeleft <= 0){
clearInterval(downloadTimer);
document.getElementById("countdown").innerHTML = "Finished";
UpdateRecord();
}
}, 1000);
</script>
</div>
<div class="second-row">
<iframe src="'.$row['adurl'].'"></iframe>
</div>
</body></html>';
然后spotlightcount.php具有以下内容:
include "config.php";
mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname) or die("Error: Unable to select database");
session_start();
if (isset($_POST['userid'])){
$userid = $_POST['userid'];
@mysql_query("Update ".$prefix."members set responsepoints=responsepoints+10, credits=credits+10 where Id=$userid limit 1") or die(mysql_error());
}
exit;
?>
现在无论我做什么,当我加载页面时,完成10秒钟的倒计时,统计信息仍然不会更新。.我找不到这是怎么回事。
答案 0 :(得分:2)
您正在使用jQuery函数($.ajax
),但尚未导入库。
在关闭正文标签之前添加:
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>