PHP表格没有显示

时间:2015-02-05 18:57:05

标签: javascript php jquery ajax

我有一个PHP脚本连接到MySQL数据库,应该用HTML输出一个表。我已经厌倦了设置一个长轮询的AJAX脚本来每秒轮询我的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);

1 个答案:

答案 0 :(得分: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);@