2015-02-05 65 views
0

我有一個PHP腳本,它連接到一個MySQL數據庫,並應輸出一個HTML表格。我已經厭倦了設置一個長輪詢的AJAX腳本來每秒輪詢我的PHP腳本。它似乎工作基於我可以在我的瀏覽器調試器中看到,但表中沒有顯示在頁面上。任何人都可以幫我找到我的代碼有什麼問題嗎?PHP表格不顯示

reocrd.php

<?php 
    header("Cache-Control: no-cache, must-revalidate"); 
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
    flush(); 
?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    </head> 
    <body> 
     <?php 
     $servername = "localhost"; 
     $username = "recorduser"; 
     $password = "recorduser"; 
     $database = "record"; 

     // Create connection 
     $conn = mysqli_connect($servername, $username, $password, $database); 
     // Check connection 
     if (!$conn) { 
      die("Connection failed: " . mysqli_connect_error()); 
     } 

     $sql = "SELECT * from username ORDER BY id DESC LIMIT 1"; 
     $result = mysqli_query($conn, $sql); 

     echo " 
     <table border='1'> 
      <tr> 
       <th>Firstname</th> 
       <th>Lastname</th> 
      </tr>"; 

     while($row = mysqli_fetch_array($result)){ 
      echo "<tr>"; 
      echo "<td>" . $row['name'] . "</td>"; 
      echo "<td>" . $row['lastname'] . "</td>"; 
      echo "</tr>"; 
      echo "</table>"; 
     } 
     mysqli_close($conn); 
     ?> 
    </body> 
</html> 

test3.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <title>Comet php backend</title> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    </head> 
    <body> 
     <head> 
      <script type="text/javascript" src="http://code.jquery.com/jquery- 1.11.2.min.js"></script> 
      <script type="text/javascript" src="client1.js"></script> 
     </head> 
     <h1>Response from server:</h1> 
    </body> 
</html> 

client1.js

setInterval(function(){ 
    $.ajax({ 
     url  : "record.php", 
     success : function(data){ 
      //Update your dashboard gauge 
      salesGauge.setValue(data.value); 
     }, 
     dataType : "json" 
    }); 
}, 30000); 
+0

因爲您嘗試將HTML塊添加到一個標準'value'屬性?無論如何,有'salesGauge'?爲什麼這些多頭?試試'success:function(data){$('body')。append(data)...'來代替。這將明顯測試PHP腳本是否返回某些內容。 – davidkonrad 2015-02-05 19:02:01

+1

你的HTML代碼有些奇怪。爲什麼雙''標籤(在'body'標籤內)?另外,爲什麼在while循環內部放置'echo'「'?你正在關閉每一行的表格。 – 2015-02-05 19:15:28

+0

@davidkonrad您證明我的代碼不起作用。 'salesGauge'是什麼意思? – user3818265 2015-02-06 14:43:07

回答

0

假設你有一個<div id="salesGuage"></div>,也許這可以工作?不要設置值屬性,請嘗試設置innerHTML

setInterval(function(){ 
$.ajax({ url: "record.php", success: function(data){ 
    //Update your dashboard gauge 
    $("#salesGuage").html(data.value); 
}, dataType: "json"}); 
}, 30000);@ 
+0

我在'index.html'中用'

'累了你的代碼,但是不起作用。 – user3818265 2015-02-06 14:44:36