首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当用户使用AJAX和jQuery登录时,如何更新php页面?

当用户使用AJAX和jQuery登录时,如何更新php页面?
EN

Stack Overflow用户
提问于 2013-04-25 14:32:58
回答 4查看 1K关注 0票数 0

当用户登录时,我想运行bellow php页面。我使用bellow php代码显示他们的名字的在线访问,我也链接他们的个人资料页面与它。

online.php重新加载时,php页面正在运行。

我知道,这是php的正常方式,但我想使用ajax来运行bellow php脚本。我知道如何使用ajax发送表单数据,但如果页面中没有表单,我不知道如何发送数据。

这怎麽可能?我试过了但还没起作用。Note:我也在使用JQuery。

AJAX代码

代码语言:javascript
复制
//update the online users list
$.ajax({
    type: "POST",
    url: "online.php",
    data: data,
    success: function (response) {
        $("#online").html(response);
    }
}); //end it

PHP代码

代码语言:javascript
复制
<?php
    session_start();

    include('controller/class/RO_dbconfig.php');

    $sth = $dbconnect->prepare("SELECT first_name,keyId FROM register WHERE status = :online");
    $params = array("online" => 1);
    $sth->execute($params);
    $status = $sth->fetchAll();

    echo "<div id='online'>";

    foreach($status as $onlineArray) {
        echo "<ul class='online'>";
        echo "<li><a href='timeline.php?profileId=".$onlineArray['keyId']."'>".$onlineArray['first_name']."</a></li>";
        echo "</ul>";
    }

    echo "</div>";
?>
EN

回答 4

Stack Overflow用户

发布于 2013-04-25 14:47:31

如果我正确地理解了您,这将帮助您检查是否有一个新用户在线,然后使用或不使用POSTed表单数据获取您的联机列表。

代码语言:javascript
复制
<script type="text/javascript">
// example variable; you can set this to the ID of the last stored online session in your DB
// the variable will be referenced later to see if any new sessions have been added
var lastConnectionId = 446;

// this function will fetch your online list; if a form has been submitted you can pass the serialized POST data
// else you can pass a null variable
function getOnlineList(data) {
    var requestType = (data == null) ? "GET" : "POST";
    $.ajax({
        type: requestType,
        url: "online.php",
        data: data,
        success: function (responce) {
            $("#online").html(responce);
        }
    });
}

// this function will check to see if any new sessions have been added (i.e. new online users) by comparing to last
// session row ID in the database to the variable lastConnectionId; there must be a table in the database that stores
// sessions and gives them incremental IDs; check-sessions.php should output the latest session ID
function checkSessions() {
    $.ajax({
        type: "GET",
        url: "check-sessions.php",
        success: function (responce) {
            if (responce > lastConnectionId) {
                lastConnectionId = responce;
                getOnlineList(null);
            }
        }
    });
}

// runs checkSessions ever 3 seconds to check for a new user
setInterval("checkSessions", 3000);
</script>

当然,在必要时添加所有其他代码和监听器等。

票数 1
EN

Stack Overflow用户

发布于 2013-04-25 14:42:14

你应该使用load()

代码语言:javascript
复制
$("#online").load("online.php");

或者你可以使用get (在你的情况下比post更好):

代码语言:javascript
复制
$.ajax({
            type: "GET",
            url: "online.php",
            data: {},

            success: function (responce) {

                $("#online").html(responce);

            }



        });
票数 0
EN

Stack Overflow用户

发布于 2013-04-25 14:45:24

当用户联机时,不能使其更新。您必须在计时器的帮助下轮询online.php。执行定期编写的代码,如下所述:Server polling with JavaScript

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16217587

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档