繁体   English   中英

使用ajax,php和html将数据库中的数据显示到表中

[英]Display data from database into a table using ajax, php and html

我正在尝试使用ajax,php和html将数据从数据库显示到表中,而无需按任何按钮或刷新。 这是shipping.php,其中是php代码:

<?php
      require_once '../core/init.php';
      if(!is_logged_in()){
        header('Location: login.php');
      }
      include 'includes/head.php';
      include 'includes/navigation.php';
      $txnQuery = "SELECT t.id, t.cart_id, t.first_name, t.last_name, t.description, t.txn_date, t.grand_total, c.items, c.paid, c.shipped
        FROM transactions t
        LEFT JOIN cart c ON t.cart_id = c.id
        WHERE c.paid = 1 AND c.shipped = 0
        ORDER BY t.txn_date";
      $txnResults = $db->query($txnQuery);

while($order = mysqli_fetch_assoc($txnResults)){
    $data = "
        <tr>
          <td><a href=\"orders.php?txn_id=". $order['id'] ."\" class=\"btn btn-sm btn-info\">Details<span class=\"glyphicon glyphicon-info-sign\"></span></a></td>
          <td>".$order['first_name'].  " ". $order['last_name'] ."</td>
          <td>".$order['description'] ." </td>
          <td>". money($order['grand_total']) ."</td>
          <td>". pretty_date($order['txn_date']) ."</td>
                </tr>
        ";
}
echo $data;
?>

这里是index.php,其中是html和ajax代码:

<?php
  require_once '../core/init.php';
  if(!is_logged_in()){
    header('Location: login.php');
  }
  include 'includes/head.php';
  include 'includes/navigation.php';

?>

<div class="col-lg-12">
  <h4 class="text-center">Orders To Ship       <span class="glyphicon glyphicon-list-alt" style="font-size:23px;"></span></h4>
  <table class="table table-sm table-bordered table-striped">
    <thead>
      <th></th><th>Name</th><th>Description</th><th>Total</th><th>Date</th>
    </thead>
    <tbody id="table-to-be-inserted">

    </tbody>
  </table>
</div>
<script>
function getdata(){
    $.ajax({
                url : "shipping.php",
                success: function(data){
                    $("#table-to-be-inserted").html(data);
                    setTimeout(getdata, 1000);
                }
        });
}
  //Call the function
   getdata();
</script>

现在,问题是只显示输入到数据库的最后数据而不是数据库中的所有数据(我在数据库中有另一个名称)。 此外,数据未显示在表格中的相应位置。 此外,在控制台中,它说主要线程上的[Deprecation]同步XMLHttpRequest因其对最终用户体验的不利影响而被弃用。 如需更多帮助,请查看https://xhr.spec.whatwg.org/ ,我不知道这是否会导致问题。

如图中所示

仅显示最后一个数据的原因是您在循环中反复替换$data的值。 一定是这样的:

while($order = mysqli_fetch_assoc($txnResults)){
    $data .= "
        <tr>
          <td><a href=\"orders.php?txn_id=". $order['id'] ."\" class=\"btn btn-sm btn-info\">Details<span class=\"glyphicon glyphicon-info-sign\"></span></a></td>
          <td>".$order['first_name'].  " ". $order['last_name'] ."</td>
          <td>".$order['description'] ." </td>
          <td>". money($order['grand_total']) ."</td>
          <td>". pretty_date($order['txn_date']) ."</td>
        </tr>
        ";
}

使用.=连接$data变量。

其次,你能准确说明它在相应元素中的显示方式吗? 喜欢,它最终会在哪里?

暂无
暂无

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

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