我试图在每次complete()发生时更新数据库值。以下代码位于名为“ user.php”的文件中,并且$ u在文件的其他位置全局定义。当运行complete()时,值pT根本不会更新。预先感谢。
<?php
if (isset($_POST['pT'])){
$pT = $_POST['pT'];
$sql = "UPDATE progress SET progressbar='$pT' WHERE username='$u'";
$query = mysqli_query($conn, $sql);
exit();
}
?>
var pT = 0;
function complete(){
pT = pT + 25;
$.ajax({
url: 'user.php',
type: 'POST',
data: {'pT': pT},
success: function() {}
});
}
答案 0 :(得分:1)
拆分文件
user.php
_______
<?php
if (isset($_POST['pT'])){
$pT = $_POST['pT'];
$sql = "UPDATE progress SET progressbar='$pT' WHERE username='$u'";
$query = mysqli_query($conn, $sql);
exit();
}
?>
然后确保您的javascript / jquery ajax没有格式错误
somewherelse.html
____________
var pT = 0;
function complete(){
pT = pT + 25;
$.ajax({
url: 'user.php',
type: 'POST',
data: 'pT='+ pT,
success: function() {
alert('done');
}
});
}