【问题标题】:AJAX to refresh user online in chat scriptAJAX 在聊天脚本中在线刷新用户
【发布时间】:2014-12-26 04:26:34
【问题描述】:

我在here 找到了我网站的聊天脚本 该脚本已经运行并刷新了每条消息,但问题是在线用户列表没有刷新每个登录的新用户。

ajax 代码

// Now let's load chatroom's active users       
function load_users(){
    // Let's use AJAX also to get chatroom's users
    $.ajax({
        url: "includes/actions.php?act=getusers",
        cache: false    
    });
}
setInterval(load_messages, 500);
setInterval(load_users, 500);  

actions.php 脚本

function get_users(){

    global $tb_satker;
    // Let's get all info from "auth" table
    $sql = "SELECT * FROM $tb_satker WHERE status!=''";
    $query = mysql_query($sql);
    if(!$query){
        echo "Can not get users from database.";
    }else{
        while($row = mysql_fetch_array($query)){
            $namasatker=$row['nama_satker'];
            echo "<li><i class=\"icon-user\"></i>$namasatker</li>";
        }
    }

}

【问题讨论】:

  • 您的函数不会在任何地方显示响应。
  • 完整的脚本可以在这里找到 forum.codecall.net/topic/49763-simple-chatroom-with-php-jquery 我认为在 load_users() ajax 脚本中遗漏了一些东西
  • 您可能需要在URL 中随机添加一些内容,以防止浏览器缓存
  • @RST 这就是cache: false 所做的
  • @Barmar 你是对的,完全忽略了那条线。格式有点不对。

标签: php mysql ajax chat


【解决方案1】:

你需要一个成功的函数来显示返回的 HTML:

function load_users(){
    // Let's use AJAX also to get chatroom's users
    $.ajax({
        url: "includes/actions.php?act=getusers",
        cache: false,
        success: function(response) {
            $("#onlineusers").html(response);
        }
    });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-05
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 2010-10-17
    • 2017-12-06
    • 1970-01-01
    相关资源
    最近更新 更多