秒后刷新表

时间:2017-05-03 08:48:13

标签: php jquery html ajax

我目前正在尝试每隔5秒刷一次我的桌子,但我没有成功。

<table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example">
  <thead>
    <tr>
      <th>Número</th>
      <th>Nome do colaborador / secção</th>
    </tr>
  </thead>
  <tbody>               
    <?php
      $req = mysqli_query($con, 'select user_id, user_name from users');                                                                        
      while ($dnn = mysqli_fetch_array($req)) {
        echo "<tr>";
        echo "    <td>" . $dnn['user_id'] . "</td>";
        echo "  <td>" . $dnn['user_name'] . "</td>";
        echo "</tr>";       
      } 
    ?> 
  </tbody>
</table>

我想要的是每5秒刷新一次页面,当然如果有人编辑,他将能够看到所做的更改。

如何完成此操作?

1 个答案:

答案 0 :(得分:1)

您需要使用$.ajax之类的

$(function(){
    function getdata(){
       $.ajax({
          url:'getdata.php',
          success:function(response){
              $('#dataTables-example tbody').html(response);
          }
       });
    }
    setInterval(function(){getdata()},5000);
});

<强>访问getdata.php

<?php
     $req = mysqli_query($con, 'select user_id, user_name from users');
     while ($dnn = mysqli_fetch_array($req)) {
        echo "<tr>
                 <td>" . $dnn['user_id'] . "</td>
                  <td>" . $dnn['user_name'] . "</td>
              </tr>";
     }   
?> 
相关问题