繁体   English   中英

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

[英]How to update a php page when a user logs in using AJAX and jQuery?

我想在用户登录时运行波纹管php页面。我正在使用波纹管php代码显示具有其名称的在线访问,并且我也将其个人资料页面与其链接。

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

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

这怎么可能? 我试过了,但是还不能用。 注意 :我也在使用JQuery。

AJAX代码

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

PHP代码

<?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>";
?>

如果我对您的理解正确,那么这应该可以帮助您检查新用户是否已经上线,然后在有或没有POST表单数据的情况下获取您的在线列表。

<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>

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

您应该使用load()

$("#online").load("online.php");

或者,您可以使用get(在您的情况下胜于发布):

$.ajax({
            type: "GET",
            url: "online.php",
            data: {},

            success: function (responce) {

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

            }



        });

用户com联机时,无法使其更新。 您必须借助计时器来轮询online.php。 定期执行烤写的代码,如此处所述: 使用JavaScript进行服务器轮询

我不确定您的实际问题是什么,所以我将尝试重述您的问题。

用户登录后,您想更新在线用户列表。 您是否想为所有当前登录的用户即时更新此列表? 因为使用php不可能(轻松)。 您可以编写一个轮询脚本,每隔几分钟检查一次。

我不确定是否需要。 我认为在每次页面加载时刷新此列表是完全可以的。 如果要将这些更改推送给用户,则需要进行长时间轮询。

任何状况之下:

AJAX代码

setInterval(function(){
    $.ajax({
        type: "POST",
        url: "online.php",
        complete: function(jqXHR, textStatus){
            $("#online").html(jqXHR.responseText);
        }
    });
},300000);

的PHP

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

这将每5分钟调用一次列表,并更新div#online。 我仍然认为这不是必需的步骤。 如果您使用哈希网址,我只会在pageLoad或hashChange上调用ajax。

脚注,此代码尚未经过测试,因此我可能在这里和那里写了一些错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM