Page 1 of 1

PHP table isn't displaying

Posted: Fri Feb 06, 2015 9:11 am
by hadi
I have a PHP script which connects to a MySQL database and should output a table in HTML. I have tired to set up a long-polling AJAX script to poll my PHP script every second. It seems to work based on what I can see in my browser debugger, however the table isn't showing on the page. Can anybody help me find what's wrong with my code?

reocrd.php

Code: Select all

<?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

Code: Select all

<!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

Code: Select all

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

Re: PHP table isn't displaying

Posted: Fri Feb 06, 2015 9:28 am
by Nable
1. You print opening tag <table> once but the closing one (</table>) is printed for each row. This is definitely wrong.
2. Space in URL: "http://code.jquery.com/jquery- 1.11.2.min.js". I wonder it was inserted during copy-pasting here or this mistake is also present in your test code.
3. "salesGauge" isn't defined anywhere.