2013-05-27 105 views
0

我新的PHP和AJAX ..自動更新從DATABSE一個表而不刷新頁面

我面臨的問題是 -

我在一個jsp頁面中顯示的表..和我想自動更新表沒有

每10秒刷新頁面。

我正在檢索值從PHP頁面數據庫..

這裏是data.jsp

<html> 
    <script type="text/javascript"> 

    function Ajax() 
    { 
     var 
      $http, 
      $self = arguments.callee; 

     if (window.XMLHttpRequest) { 
      $http = new XMLHttpRequest(); 
     } else if (window.ActiveXObject) { 
      try { 
       $http = new ActiveXObject('Msxml2.XMLHTTP'); 
      } catch(e) { 
       $http = new ActiveXObject('Microsoft.XMLHTTP'); 
      } 
     } 

     if ($http) { 
      $http.onreadystatechange = function() 
      { 
       if (/4|^complete$/.test($http.readyState)) { 
        document.getElementById('ReloadThis').innerHTML =    $http.responseText; 
        setTimeout(function(){$self();}, 10000); 
       } 
      }; 
      $http.open('GET', 'getuser.jsp', true); 
      $http.send(null); 
     } 
    } 
    function myFunction() 
      { 
     setTimeout(function() {ajax();}, 10000); 
      } 
    </script> 
     </head> 

     <body> 

     <button onclick="myFunction()">Try it</button> 
      <div id="ReloadThis" > the table is to be shown here </div> 
      </body> 
      </html> 

的代碼,因爲我的tomcat WZ做的getUser部分php..but犯規supprt PHP的..所以我將我的代碼把jsp ..這裏是我的代碼

和代碼getuser.jsp

  <%@page import="java.sql.ResultSet"%> 
     <%@page import="java.sql.PreparedStatement"%> 
     <%@page import="java.sql.DriverManager"%> 
      <%@page import="java.sql.Connection"%> 

      <% 

        String cond="SELECT * FROM invertor "; 
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
        Connection con = DriverManager.getConnection("jdbc:odbc:xe"); 
        PreparedStatement ps = con.prepareStatement(cond); 

        ResultSet rs = ps.executeQuery(); 

        if (rs.next()) 
        { 
        dc_volt = rs.getString(1).trim(); 
        dc_amp = rs.getString(2).trim(); 
        ac_volt = rs.getString(3).trim(); 

        ac_amp = rs.getString(4).trim(); 

        } 
        else 
        { 

        } 

       %> 

,我得到的錯誤是java.lang.arrayindexoutofboundsexception:觸發「試試看」的後2

:從我data.jsp頁..沒有DATABSE表顯示按鈕...

+0

你一直面臨的問題是什麼? – Sanjeev

+0

你正在聲明'$ q = $ _ GET [「q」];'但是不使用它,你想在數據庫中搜索並返回值嗎? – 0xAli

+0

我編輯了我的php ..檢查它..和問題是我沒有得到任何更新的結果.. –

回答

0

您的代碼應該是這樣的 -

<script> 
if ($http) { 

    /*make the call*/ 
    $http.open('GET', 'getuser.php', true);    

    /* check for state change */ 
    $http.onreadystatechange = function() 
      { 
       if (/4|^complete$/.test($http.readyState)) { 
        document.getElementById('ReloadThis').innerHTML =    $http.responseText; 
        setTimeout(function(){$self();}, 10000); 
       } 
      }; 

    /*Because this is a GET request, the send values can be null*/  
    $http.send(null); 
} 
</script> 
+0

我試圖你attmpt,但soory說,它沒有幫助 –