2013-01-03 76 views
-3

一個很好的例子就是facebook。有人評論他人後,所有人都可以看到更新彈出不只是你自己的網頁,並不需要刷新..我做了一個項目,監視或篩選出不好的評論..所以,如果有一個錯誤的評論插入數據庫然後它給我通知並彈出數據..任何幫助請..謝謝我使用PHP的服務器端語言。如何在數據庫插入或更新php或ajax後更新頁面

+2

使用AJAX通過'setInterval'檢查更新。 Google是你的朋友。 – Cerbrus

+0

你寫過「..請任何幫助..」,但如果你不評論或回覆,那麼你將得到什麼樣的幫助。 –

回答

0

您將需要從您的頁面發出AJAX請求來實現此目的。

當用戶執行操作時,您可以提交ajax請求並動態更新該用戶的頁面。其他人也會看到這一點。

+0

'其他人也會看到' - 基於單個用戶的操作。當然,這需要每個人的頁面輪詢服務器的更新? – PassKit

0

有一個很好的PHP/MySQL/AJAX教程和下載可用here應該讓你開始。結合你的服務器端過濾邏輯和週期性的Ajax回調來保持每個人的頁面更新,你應該能夠實現你正在尋找的東西。

1

是的,總有一種方法。 您可以使用彗星編程和使用ajax和php進行長時間輪詢。

很好的缺點是使用彗星你的其他http請求將是Holt所以對於慧星你必須重寫最好的會話。

根據你的問題,我建議你使用myisam引擎,因爲它跟蹤數據庫最後修改時的時間戳,以便您知道數據庫何時更新。

樣本彗星代碼如下。

的index.php

<div id="content"></div> 

<p id="form_container"> 
    <form action="" method="get" onsubmit="comet.doRequest($('#word').val());$('#word').val('');return false;"> 
     <input type="text" name="word" id="word" value="" /> 
     <input type="submit" name="submit" value="Send" /> 
    </form> 
</p> 
<script type="text/javascript" src="js/jquery.js"></script> 
<script type="text/javascript"> 

    var Comet = function (data_url) 
    { 
     this.timestamp = 0; 
     this.url = data_url; 
     this.noerror = true; 

     this.connect = function() 
     { 
      var self = this; 

      $.ajax(
      { 
       type : 'post', 
       url : this.url, 
       dataType : 'json', 
       data : 
       { 
        'timestamp' : self.timestamp 
       }, 
       success : function(response) 
       { 
        self.timestamp = response.timestamp; 
        self.handleResponse(response); 
        self.noerror = true;   
       }, 
       complete : function(response) 
       { 
        if (!self.noerror) 
        { 
         setTimeout(function(){ comet.connect(); }, 5000);   
        } 
        else 
        { 
         self.connect(); 
        } 
        self.noerror = false; 
       } 
      }); 
     } 

     this.disconnect = function() {} 

     this.handleResponse = function(response) 
     { 
      $('#content').append('<div>' + response.msg + '</div>'); 
     } 

     this.doRequest = function(request) { 
      $.ajax(
      { 
       type : 'post', 
       url : this.url, 
       data : 
       { 
        'msg' : request 
       } 
      }); 
     } 
    } 

    var comet = new Comet('./backend.php'); 
    comet.connect(); 
</script>

backend.php

function get_date() 
{ 
    $query = mysql_query("show table status from weefavr like 'request_responses'")or die(mysql_error()); 

    $row = array(); 
    while($result = mysql_fetch_assoc($query)) 
    { 
     $row[] = $result; 
    } 
    return strtotime($row[0]['Update_time']); 
} 

$lastmodif = isset($_POST['timestamp']) ? $_POST['timestamp'] : 0; 
$currentmodif = get_date(); 

while ($currentmodif <= $lastmodif) 
{ 
    usleep(10000); 
    clearstatcache(); 
    $currentmodif = get_date(); 
} 

$query_db = mysql_query("SELECT UNIX_TIMESTAMP(last_update.created) AS DATE,last_update.* FROM last_update WHERE UNIX_TIMESTAMP(last_update.created) BETWEEN '".$lastmodif."' AND '".$currentmodif."'")or die(mysql_error()); 

$row_db = array(); 
$response = array(); 
while($result_db = mysql_fetch_assoc($query_db)) 
{ 
    $response['msg'][]  = $result_db['title']; 
} 

$response['timestamp'] = $currentmodif; 
$response['lastmodif'] = $lastmodif; 
echo json_encode($response); 
flush();

這個例子是非常有用的,但我告訴你,你的其他的HTTP請求將無法正常工作。 所以你需要做一些關於會議和其他方面的黑客攻擊。

是的Facebook,gmail,asana和zaarly是使用彗星編程的大巨人。

希望這會有所幫助。 隨時問問歡呼...

0

這將是PHP和Ajax。 如果您需要它,基本introduction在這裏。 基本上你需要有一個網頁,通過JavaScript異步輪詢您的後端(PHP頁面),提供新的內容。你可以有一個Javascript計時器,甚至更復雜的設置,如後端「推送」到你的頁面。 我建議您查看其他page的示例。