So, I'm trying to learn Jquery / AJAX, using an array to execute and update code for different elements within a single function.
Below I am trying to update DIVs by the number of registered on the site and the total currency in circulation. The script will be updated every few seconds.
Can someone help me fix my syntax or at least tell me what I'm doing wrong here?
<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();
?>
source
share