为什么赢得这个ajax柜台显示器?

时间:2014-09-27 22:35:02

标签: php ajax

我想在我的网站上显示有多少人在线。但是ajax代码并没有在我的div中显示任何内容,我不知道为什么会这样。

<script type="text/javascript">
  $(document).ready(function() {

  //START After 5 minutes update database timestamp (this part of script works)
  fnShowImOnline();
  setInterval('fnShowImOnline', 120000);

  function fnShowImOnline() {
    $.get('counter_im_online.php');
  }
  //END 
  {
  $.ajax({url: 'counter_members_online.php', dataType: 'json', success: function(response) {
    if (isNumeric(response.total)) {
      $('#OnlineTotal').html(response.total + " Total ");
      $('#OnlineNow').html(response.online +  " Online Now");
    }
  }
});
</script>

<div id="OnlineTotal"></div><div id="OnlineNow"></div>

<?php 
// Contents of counter_members_online.php:
// Members online.
$online_sql = "SELECT COUNT(*) FROM users where last_checked_in > DATE_SUB(NOW(), INTERVAL 5 MINUTE)";
$online_RS  = mysql_query($online_sql);
$online_row = mysql_fetch_row($online_RS);
$online     = $online_row[0];

// Members total.
$total_sql = "SELECT COUNT(*) FROM users";
$total_RS  = mysql_query($total_sql);
$total_row = mysql_fetch_row($total_RS);
$total     = $total_row[0];
$response  = json_encode(array('total'=>$total,'online'=>$online));
echo($response);
?>

2 个答案:

答案 0 :(得分:0)

不应该是这段代码

$.ajax({url: 'counter_members_online.php', dataType: 'json', success: function(response) {
        if (isNumeric(response.total)) {
            $('#OnlineTotal').html(response.total + " Total ");
            $('#OnlineNow').html(response.online +  " Online Now");
        }
    }
});

function fnShowImOnline()

的一部分

即。 function fnShowImOnline应该阅读

function fnShowImOnline() {
$.ajax({url: 'counter_members_online.php', dataType: 'json', success: function(response) {
        if (isNumeric(response.total)) {
            $('#OnlineTotal').html(response.total + " Total ");
            $('#OnlineNow').html(response.online +  " Online Now");
        }
    }
});
}

你可以尝试用以下代码替换你的标签:

<script type="text/javascript">
  $(document).ready(function() {

  //START After 5 minutes update database timestamp (this part of script works)
  fnShowImOnline();
  setInterval('fnShowImOnline', 120000);
});

    function fnShowImOnline() {
    $.ajax({url: 'counter_members_online.php', dataType: 'json', success: function(response) {
            if (isNumeric(response.total)) {
                $('#OnlineTotal').html(response.total + " Total ");
                $('#OnlineNow').html(response.online +  " Online Now");
            }
        }
    });
    }

</script>

答案 1 :(得分:0)

我发现了问题。下面的脚本现在可以使用,$ .ajax部分在它之前有一个}并且它不起作用:

<script type="text/javascript">
  $(document).ready(function() {

  // After 5 minutes update database timestamp (this part of script works)
  fnShowImOnline();
  setInterval('fnShowImOnline', 120000);
});

    function fnShowImOnline() {
    $.get('counter_im_online.php');
    }

$.ajax({
    url: 'counter_members_online.php',
    dataType: 'json',
    success: function(response) {
        if (!isNaN(response.total)) {
            $('#OnlineTotal').html(response.total + " Total ");
            $('#OnlineOnline').html(response.online +  " Members Online");
        }
    }
})
</script>