[英]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.