当用户使用AJAX和jQuery登录时,如何更新php页面?
我想在用户登录时运行下面的php页面。我使用下面的php代码来显示他们的名字的在线访问,我还将他们的个人资料页面与之链接 重新加载当用户使用AJAX和jQuery登录时,如何更新php页面?,php,javascript,jquery,html,ajax,Php,Javascript,Jquery,Html,Ajax,我想在用户登录时运行下面的php页面。我使用下面的php代码来显示他们的名字的在线访问,我还将他们的个人资料页面与之链接 重新加载online.php时,php页面正在运行 我知道这是php的正常方式,但我想使用ajax来运行下面的php脚本。我知道如何使用ajax发送表单数据,但如果页面中没有任何表单,我不知道如何发送数据 这怎么可能?我试过了,但还不行注意:我也在使用JQuery AJAX代码 //update the online users list $.ajax({ type:
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
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>";
?>
setInterval(function(){
$.ajax({
type: "POST",
url: "online.php",
complete: function(jqXHR, textStatus){
$("#online").html(jqXHR.responseText);
}
});
},300000);
PHP代码
//update the online users list
$.ajax({
type: "POST",
url: "online.php",
data: data,
success: function (response) {
$("#online").html(response);
}
}); //end it
<?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>";
?>
setInterval(function(){
$.ajax({
type: "POST",
url: "online.php",
complete: function(jqXHR, textStatus){
$("#online").html(jqXHR.responseText);
}
});
},300000);
您应该使用load()
:
或者您可以使用get(在您的情况下比post更好):
当用户联机时,您无法使其更新。您必须在计时器的帮助下轮询online.php。执行定期编写的代码,如此处所述:如果我理解正确,这将帮助您检查新用户是否已联机,然后获取联机列表,其中包含或不包含已发布的表单数据
<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>
//示例变量;您可以将其设置为数据库中上次存储的联机会话的ID
//稍后将引用该变量,以查看是否添加了任何新会话
var lastConnectionId=446;
//此功能将获取您的在线列表;如果表单已提交,则可以传递序列化的POST数据
//否则可以传递一个空变量
函数getOnlineList(数据){
var requestType=(数据==null)?“GET”:“POST”;
$.ajax({
类型:requestType,
url:“online.php”,
数据:数据,
成功:功能(响应){
$(“#在线”).html(响应);
}
});
}
//此功能将通过与上一个会话进行比较来检查是否添加了任何新会话(即新的在线用户)
//数据库中会话行ID到变量lastConnectionId;数据库中必须有一个存储
//会话,并为其提供增量ID;php应该输出最新的会话ID
函数checkSessions(){
$.ajax({
键入:“获取”,
url:“check sessions.php”,
成功:功能(响应){
如果(Response>lastConnectionId){
lastConnectionId=响应一次;
getOnlineList(空);
}
}
});
}
//每3秒钟运行一次检查会话以检查新用户
设置间隔(“检查会话”,3000);
当然,在必要时添加所有其他代码和侦听器等。我将尝试重新表述您的问题,因为我不确定您的实际问题是什么
当用户登录时,您希望更新联机用户列表。
是否要为所有当前登录的用户动态更新此列表?因为使用php不可能(很容易)做到这一点。
您可以编写一个轮询脚本,每隔几分钟检查一次
我不确定是否需要这样做。我认为在每次页面加载时刷新此列表是完全可以的。如果您想将这些更改推送给用户,您需要查看长轮询
无论如何:
AJAX代码
//update the online users list
$.ajax({
type: "POST",
url: "online.php",
data: data,
success: function (response) {
$("#online").html(response);
}
}); //end it
<?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>";
?>
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>";
?>
'data:data'…数据变量的值是什么?在ajax中,读取data:data
的行,第二个数据信息来自哪里?这是我不知道如何做的事情?是否发送输入值?不,我想在用户联机时使用ajax运行该online.php页面。