我正在做的是我相信基本的,但我似乎无法让它发挥作用。我有一个名为current.php的页面,它接受用户并从MySQL数据库中提取附加到该用户的号码。当手动转到页面current.php?user = test时,数字拉得很好,但是当我转到主页user.html时,数字没有拉动。我正在使用一个设置间隔的jQuery,但是,我是新手,所以我不确定断开连接的位置。目标是编辑数据库并让user.html反映数据库中的更改。这是我的两页:
current.php
<?php
$servername = "xxx";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$user = $_GET['user'];
$sql = "SELECT name, win FROM streamer WHERE name = '$user' LIMIT 0,1";
$result = $conn->query($sql);
if ($result->num_rows == 1) {
$row = $result->fetch_assoc();
echo $row['win'];
}
else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
user.html
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function updateNum() {
var url="current.php?user=test";
jQuery("#number").load(url);
}
setInterval("updateNum()", 5000);
</script>
</head>
<body>
Here is the current number: <div id="number"></div>
</body>
</html>
编辑:将我的user.html更改为如下后,问题解决了。
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"> </script>
<script>
$(document).ready(function () {
setInterval(function(){f()},3000);
function f(){
$("#div1").load("current.php?user=test");
}
});
</script>
</head>
<body>
<div id="div1"><h2></h2></div>
</body>
</html>