php使用jQuery数据表以json格式从数据库中获取数据

时间:2017-11-22 11:51:13

标签: javascript php jquery datatables

当我选择经销商名称和日期时,然后按下它使用jquery将请求发送到php文件的按钮。我测试了php文件,它正在以正确的方式检索数据,但我想在这个数据表中显示返回数据。这个jQuery发送数据但没有显示返回结果。我在这段代码中做错了什么?

$(document).ready(function() {
  $("#date").datepicker({
    prevText: '<i class="fa fa-arrow-left"></i>',
    nextText: '<i class="fa fa-arrow-right"></i>',
    constrainInput: true,
    dateFormat: 'yy-mm-dd'
  });

  $("#submit").click(function() {
    var datepicker = $("#date").val();
    var Dis = $('#dist_nam :selected').val();
    // Returns successful data submission message when the entered information is stored in database.
    var data = 'd=' + datepicker + '&q=' + Dis;
    if (datepicker == '' || Dis == '') {
      alert("Please Fill All Fields");
    } else {
      $('#example').DataTable({
        "processing": true,
        "serverSide": true,
        "ajax": 'Distributor.php',
        cache: false,
        "columns": [
          // rest of the columns
          {
            data: "distributor_name"
          }, {
            data: "order_date"
          }, {
            data: "product_name"
          }, {
            data: "nsp"
          }, {
            data: "current-sales"
          }, {
            data: "closing-balance"
          }
        ]

      });
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css" rel="stylesheet" />
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<link href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet" />
<select name="distributor_name" class="form-control" id="dist_nam">
                                        <option value="">Select a Distributor:</option>
                                        <option value="1 ">NATIONAL</option><option value="2 ">Sunny Enterprises</option><option value="3 ">Sajjad Enterprises</option><option value="4 ">Hassan Traders</option><option value="5 ">Watan Traders</option><option value="6 ">Saeed Medicine</option><option value="7 ">Asad & Co.
</option><option value="8 ">Ali Corporation</option><option value="9 ">Pharma Link</option><option value="10 ">Pharma Link(SKT)</option><option value="12 ">Moazzam</option><option value="13 ">A.A Sons Pharmacy</option><option value="14 ">Ali Enterprises</option><option value="15 ">Ahmed Traders</option><option value="16 ">Medvax Pharma</option></select>
<div class="input-group date" data-date-format="mm-dd-yyyy">
  <input name="date" id="date" class="form-control" type="text" readonly />
  <span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
</div>
<button type="button" id="submit">View Entry</button>

<table id="example">
  <thead>
    <tr>
      <th>Distributor Name</th>
      <th>Date</th>
      <th>Product Name</th>
      <th>NSP</th>
      <th>Current Sales</th>
      <th>Closing Balance</th>
    </tr>
  </thead>
  <tfoot>
    <tr>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </tfoot>
  <tbody>
  </tbody>
</table>

  

Distributor.php(工作正常测试!)

<?php
require('db.php');
$q = intval($_GET['q']);
$time = strtotime($_GET['d']);
$day   = date('d',$time);
$month = date('m',$time);
$year  = date('Y',$time);
echo  $month + $year;

$query = "SELECT  distributor.distributor_name, orders.order_date, `products`.`product_name`, `products`.`nsp`,
`orders`.`current-sales`,
`orders`.`closing-balance`
FROM `products`
LEFT JOIN `orders` ON `orders`.`pro_id` = `products`.`pro_ID`
LEFT JOIN `distributor` ON `orders`.`d_id` = `distributor`.`d_ID`
    WHERE MONTH(orders.order_date) =  '" . $month . "'  AND YEAR(orders.order_date) = '" . $year . "' AND distributor.d_id  = '" . $q . "';";
$result = $con->query($query);
$array=array();
while($row= mysqli_fetch_array($result))
{
   $array['data'][] = $row;
}
echo json_encode($array);
$con->close();
?>

如果我硬编码$ date =“2017-10-12”和$ q = 4的值 Distributor.php ,则会显示此结果Distributor.php result。 db.php文件是连接文件。已经测试

0 个答案:

没有答案