2012-11-01 38 views
0

我遇到問題並需要解決方案。我有一個服務器發送事件,當數據庫發生變化時,每次都更新爲div ID。現在我的問題是如何從該div id中獲取值並將其保存到PHP變量中。將jquery div id值存儲到php變量中

<script> 
    if(typeof(EventSource)!=="undefined") 
    { 
     var source=new EventSource("bookcounterevent.php"); 
     source.onmessage=function(event) 
     { 
      document.getElementById("bookcounter").innerHTML=event.data; 
     }; 
    } 
    else 
    { 
    document.getElementById("bookcounter").innerHTML="Please update your browser!!"; 
    } 
    </script> 
    <div id="bookcounter"></div> 

以上代碼更新<div id="bookcounter"></div>,我想這是我收到的ID bookcounter的價值,我想將它保存到一個PHP變量說$book。如何做到這一點?

在此先感謝。

+0

將數據發送到瀏覽器並將其發送回來是沒有意義的。把它從PHP函數中發送出去。如果你更好地描述了你正在嘗試做的事情,會有所幫助 – charlietfl

+0

我不認爲你可以將它存儲在一個PHP變量(在該頁面上),因爲該頁面已經'執行',所以PHP過程已經完成一次結果是返回到瀏覽器(除非你正在做'長輪詢')。你可以做什麼是存儲在一個隱藏的領域,所以你可以將它張貼回服務器,如果需要或使用會話 –

+0

@ R.S我正在使用一個長輪詢方法,所以現在告訴我如何從那裏前進。 – user1731476

回答

1
<style> 
#notification { 
    color:green; 
} 
#nonotification { 
    color:red; 
} 
</style> 
<script> 
     if(typeof(EventSource)!=="undefined") 
     { 
      var source=new EventSource("bookcounterevent.php"); 

      source.onmessage=function(event) 
      { 
       if(parseInt(event.data) > 0){ 
       document.getElementById("bookcounter").innerHTML=event.data; 
       } 
       else{ 
       document.getElementById("nonotification").innerHTML= 'nothing to display'; 
       } 
      } 
     } 
     else 
     { 
     document.getElementById("bookcounter").innerHTML="Please update your browser!!"; 
     } 
    </script> 

<div id="notification"></div> 
<div id="nonotification"></div>