javascript - 更新 html 表 AJAX PHP

标签 javascript php html ajax

我有一个 HTML 表格,我想在页面上每 1 秒更新一次。它里面有很少的 div 和类。所以我尝试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);
 ?>

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

最佳答案

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

关于javascript - 更新 html 表 AJAX PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52464488/

相关文章:

JavaScript, AngularJS : How do I know if element is larger than its container?

javascript - 字符串数组到树数据结构

javascript - 从最后一个值更新位置/转换(仪表图/d3.js)

php - 如何在公共(public) header 中使用类="active"?

javascript - html 将整个 Canvas 旋转90度

javascript - 如何为不同的承载 token 声明2个axios实例?

php - 存储过程返回空结果

php - 重新排列多维数组(添加一维)

jquery - 漂亮的饼图菜单 jquery 中的子菜单

javascript - 无法摆脱 div 上的白边