2013-04-25 52 views
0

我想在用戶登錄時運行bellow php頁面。我使用bellow php代碼來顯示他們的名字的在線訪問,並且我還將它們的個人資料頁面與它聯繫起來。如何在用戶使用AJAX和jQuery登錄時更新php頁面?

online.php重新加載時,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>"; 
?> 
+1

'data:data'...數據變量的值是多少? – pmandell 2013-04-25 14:34:30

+1

在您的ajax中,讀取'data:data'的行,其中第二個數據信息來自哪裏? – 2013-04-25 14:34:57

+0

這是我不知道該怎麼做的事情? – wasana 2013-04-25 14:35:51

回答

0

你應該使用load()

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

或者您可以使用get(在你的情況更好THA n post):

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

      success: function (responce) { 

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

      } 



     }); 
1

如果我理解正確,這應該可以幫助您檢查新用戶是否已聯機,然後通過或不通過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> 

當然,在必要時添加所有其他代碼和偵聽器等。

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#線上。我仍然認爲這不是必需的一步。我只是在pageLoad上調用ajax,或者在hashChange上調用哈希URL。

腳註,這段代碼還沒有經過測試,所以有可能我在這裏和那裏寫了一些錯誤。

相關問題