0

我想在用户登录时运行波纹管 php 页面。我正在使用波纹管 php 代码来显示带有他们的名字的在线访问,并且我还链接了他们的个人资料页面。

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

这是php的正常方式,我知道,但我想使用ajax来运行bellow 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>";
?>
4

4 回答 4

1

如果我对您的理解正确,这应该可以帮助您检查是否有新用户上线,然后获取您的在线列表,无论是否包含已发布的表单数据。

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

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

于 2013-04-25T14:47:31.793 回答
0

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

于 2013-04-25T14:45:24.887 回答
0

我将尝试改写您的问题,因为我不确定您的实际问题是什么。

当用户登录时,您要更新在线用户列表。您想为所有当前登录的用户即时更新此列表吗?因为这不是(很容易)使用 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。

脚注,这段代码没有经过测试,所以我可能在这里和那里写了一些错误。

于 2013-04-25T14:59:03.120 回答
0

你应该使用load()

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

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

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

            success: function (responce) {

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

            }



        });
于 2013-04-25T14:42:14.017 回答