更新html表AJAX PHP

时间:2018-09-23 08:45:40

标签: javascript php html ajax

我有一个HTML表,我想每1秒更新一次。它里面没有几个div和class。所以我尝试了AJAX每1秒更新一次。 HTML是这样的:-

  <div class="abcd">
<div style='float: left;'>
<br><br>
<p style="padding-left:16px; font-size: 20px;">Amount(<?php echo $market; ?>) | Price(<?php echo $bm; ?>) &nbsp | Total(<?php echo $bm; ?>)</p>
<div class="panel-hello scrollbar" id="style-11">
    <div class="data-table">
        <table class="table table-hello table-bordered table-hover force-overflow" id="btcaddresses">
            <tbody style="border: 1px solid green; height: 300px; overflow-y: scroll;">

            </tbody>
        </table>
    </div>
</div>
</div>

和AJAX脚本:-

  function loadXMLDoc()
  {
   var xmlhttp;
  if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
   xmlhttp=new XMLHttpRequest();
   }
  else
    {// code for IE6, IE5
   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }
 xmlhttp.onreadystatechange=function()
 {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
   {
  document.getElementsById("btcaddresses").innerHTML=xmlhttp.responseText; // your div
   }
   }
xmlhttp.open("GET","getdatabase.php",true); //your php file
xmlhttp.send();
 }
 window.setInterval(function(){
   loadXMLDoc();
 }, 1000);

getdabase.php包含:-

 <?php
 require('../setup.php');
 $seql = "select price, sum(total), sum(aleft) from trade where status = 'active' and bm = 'USD' and m = 'BTC' and type = 'sell' group by price";
     $query100 = mysqli_query($conn, $seql);

while ($row = mysqli_fetch_array($query100))
    {
        echo '<tr style="cursor: pointer; font-size: 15px;">
                <td>'.number_format($row['sum(aleft)'], 8).'</td>
                <td>'.number_format($row['price'], 8).'</td>
                <td>'.number_format($row['sum(total)'], 8).'</td>
            </tr>';
    }
 mysqli_close($conn);
 ?>

问题是,即使它没有在class中指定任何表类,它也不起作用。

1 个答案:

答案 0 :(得分:1)

<?php
 require('../setup.php');
 $seql = "select price, sum(total), sum(aleft) from trade where status = 'active' and bm = 'USD' and m = 'BTC' and type = 'sell' group by price";
     $query100 = mysqli_query($conn, $seql);

$result = '';

while ($row = mysqli_fetch_array($query100))
    {
        $result .= '<tr style="cursor: pointer; font-size: 15px;">
                <td>'.number_format($row['sum(aleft)'], 8).'</td>
                <td>'.number_format($row['price'], 8).'</td>
                <td>'.number_format($row['sum(total)'], 8).'</td>
            </tr>';
    }
 mysqli_close($conn);

echo $result;
 ?>

这应该可以,但是实际上您应该返回一个json。

编辑:完整的html代码对我来说很好:(我必须删除一些php部分)

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>

    <div class="abcd">
        <div style='float: left;'>
            <br>
            <br>
            <!-- <p style="padding-left:16px; font-size: 20px;">Amount(<?php echo $market; ?>) | Price(<?php echo $bm; ?>) &nbsp | Total(<?php echo $bm; ?>)</p> -->
            <div class="panel-hello scrollbar" id="style-11">
                <div class="data-table">
                    <table class="table table-hello table-bordered table-hover force-overflow" id="btcaddresses">
                        <tbody style="border: 1px solid green; height: 300px; overflow-y: scroll;">

                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <script type="text/javascript">
            function loadXMLDoc() {
                var xmlhttp;
                if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                    xmlhttp = new XMLHttpRequest();
                }
                else {// code for IE6, IE5
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.onreadystatechange = function () {
                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                        document.getElementById("btcaddresses").innerHTML = xmlhttp.responseText; // your div
                    }
                }
                xmlhttp.open("GET", "getdatabase.php", true); //your php file
                xmlhttp.send();
            }
            window.setInterval(function () {
                loadXMLDoc();
            }, 1000);
        </script>
</body>

</html>