gpt4 book ai didi

php - 使用 Jquery、AJAX、PHP 和数组进行实时更新

转载 作者:搜寻专家 更新时间:2023-10-31 20:48:59 24 4
gpt4 key购买 nike

因此,我正在尝试使用数组学习 Jquery/AJAX,以在单个函数内的不同元素上执行和更新代码。

下面,我尝试用登录网站的人数和流通的货币总量来更新 DIV。该脚本将每隔几秒更新一次。

任何人都可以帮我纠正我的语法或者至少告诉我我在这里做错了什么吗?

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="../jquery.js"></script>

<script type="text/javascript">
function updateStats(stat)
{
var stat = ["online","money"];

var url = "online.php";

$.each(stat,function(){

$.post(url,{stat: stat} , function(data) {

$("#" + this).html(data);
})

})
}

setInterval('updateStats("updateStats")', 2000);
</script>


<body onLoad="updateStats(stats);">

<div id="online"></div>
<div id="money"></div>
</body>
</html>

<?php

if($_POST['stats']=='online')
{
$result= $mysqli->query("SELECT loggedin FROM accounts WHERE loggedin !=0");
echo $result->num_rows;
}

elseif($_POST['stats'] == 'money')
{
$result = $mysqli->query("SELECT sum(money) AS totalMoney FROM users");
$getData = $result->fetch_assoc();

echo number_format($getData['totalMoney']);

}

$mysqli->close();

?>

最佳答案

您的代码应如下所示:

function updateStats(stat)
{
var stat = ["online","money"];
var url = "online.php";

$.each(stat, function(i, key){
$.post(url, {stats: key}, function(data) {
$("#" + key).html(data);
});
});
}

另请注意复数形式的 stats 参数。

关于php - 使用 Jquery、AJAX、PHP 和数组进行实时更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10197490/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com