简体   繁体   English

使用AJAX / Javascript / jQuery自动更新HTML / PHP表以淡入新结果并淡出旧结果

[英]Automatically update a HTML/PHP table to fade in new results and fade out old results using AJAX/Javascript/jQuery

I'm looking for the simplest possible method to automatically update the below every X seconds to fade in new results and fade out old results. 我正在寻找最简单的方法来每隔X秒自动更新以下内容,以淡出新结果并淡出旧结果。

I have searched online but everything that I have found has been extremely complex so I was wondering what would be the simplest possible method to achieve this? 我已经在网上搜索过,但是发现的一切都非常复杂,所以我想知道实现这一目标的最简单方法是什么?

  <?php

  error_reporting(E_ALL);
  ini_set('display_errors', 1);

  include('core/connection.php');

  if($conn){ 

  $stid = oci_parse($conn, "

    SELECT OrderNo, InvoiceNo
    FROM Orders
    WHERE rownum <= 5
    ORDER BY rownum DESC

  ");
  oci_execute($stid);

  echo "<table class='table table-hover '>
        <thread>
        <tr>
        <th>OrderNo</th>
        <th>InvoiceNo</th>
        <th></th>
        </tr>
        </thread>
        <tbody>";

  while ($row = oci_fetch_array($stid, OCI_ASSOC)) {

    echo "<tr>";
    echo "<td>" . $row['OrderNo'] . "</td>";
    echo "<td>" . $row['InvoiceNo'] . "</td>";
    echo "</tr>";
    unset($row);

  }

  echo "</tbody>
        </table>";

  oci_free_statement($stid);
  oci_close($conn);

  }

  ?>

You need to use AJAX, it's the only viable way to load new data without refreshing a page. 您需要使用AJAX,这是加载新数据而不刷新页面的唯一可行方法。 What you've described is a common use of AJAX and there are many tutorials online, so I won't bother reinventing the wheel. 您所描述的是AJAX的常用用法,并且在线上有许多教程,因此我不会费心重新设计轮子。

This is one that I found that teaches you how to load the data, and animate it in. 我发现这是一个教您如何加载数据和对其进行动画处理的方法。

function getNewData(){
    //hide the original data
    $('.table.table-hover').hide(500, function(){
        var $this = $(this); //cache reference to the original table
        $.ajax({
            type: 'GET', //get or post request
            url: '/path/to/my/script.ext', //path to your script
            success: function(data){ //if no issues performing the request
                $this.replaceWith(data); //replace the original table with the new table data
            }
        });
    });
} 
setTimeout(function(){
    getNewData();
}, 5000); //update every 5 seconds

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM