使用PHP和AJAX在表中填充数据库中的值

时间:2019-03-28 01:38:55

标签: php ajax

我有一个包含订单的表,我希望每个订单进入数据库后都希望在不刷新页面和不按任何按钮的情况下将其显示在表中。这是PHP和HTML中的代码:

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

?>
<!-- Orders To Fill -->
<?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);
?>

<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>
      <?php while($order = mysqli_fetch_assoc($txnResults)): ?>
        <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>
      <?php endwhile; ?>
    </tbody>
  </table>
</div>

请,如何在AJAX中做到这一点?

1 个答案:

答案 0 :(得分:1)

首先尝试首先研究如何使用Jquery AJAX。很简单 创建包含对数据库请求的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;
?>

然后创建前端文件,该文件将请求您创建的php文件

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

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

这是一个简单的请求。这是Jquery Ajax的链接,请仔细研究CLICK ME TO STUDY JQUERY AJAX!!!!