PHP PDO 使用下拉菜单过滤数据

标签 php html mysql ajax pdo

如何根据过滤结果重新绘制数据表。 我通过其 ID.branch id 作为 bid 并使用outlet 作为 oid 以及用户 id 作为 uid 使用下拉搜索。这是我的代码。 我只得到了索引:未定义绘制,后面是数组结果。

  <label>Branch</label>
  <select id="branch" class="form-control">
  <option value="">Select Branch</option>
  <option value="1'">First Branch</option>
  <option value="2'">Second Branch</option>
  <option value="3'">Third Branch</option>
  </select>
  <!----Table Item Data------>
  <br/>
    <div class="container box" style="width:100%;">
        <div class="table-responsive">      
            <table id="itemdata" class="table table-bordered table-striped" 
 width="100%">
                <thead>
                    <tr>
                    <th width="10%">Branch</td>
                    <th width="10%">Outlet</td>
                    <th width="15%">User</td>
                    <th width="15%">Item</td>
                    <th width="10%">Brand</td>
                    <th width="10%">Serial No.</td>
                    <th width="10%">Purchase Date</td>
                    <th width="20%">Remarks</td>
                    </tr>
                </thead>
                <tfoot>
                    <tr>
                    <th>Branch</td>
                    <th>Outlet</td>
                    <th>User</td>
                    <th>Item</td>
                    <th>Brand</td>
                    <th>Serial No.</td>
                    <th>Purchase Date</td>
                    <th>Remarks</td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>

 <script>
$(document).ready(function () {
var dataTable = $('#itemdata').DataTable({
    "dom": '<"#buttons">lrti<"clear">',
    "bFilter": true,
    "bInfo": false,
    "scrollY":        "50%",
    "footer": true,
    "header": true,
    "scrollCollapse": true,
    "processing":true,
    "serverSide":true,
    "paging": false,
    "order":[],
    "ajax":{
        url:"include/fetch_data.php",
        type:"POST"
    },
    "columnDefs":[
        {
            "targets":[0,1,2,3,4,5,6,7],
            "orderable":false,
        },
    ],
});

//Filter Data Table By Branch   
$('#branch').on('change',function(){
    var branch = $(this).val();
    if(branch){
        var request = $.ajax({
            type:'POST',
            url:'include/fetch_data.php',
            data:'bid='+branch,
            success:function(data){
                dataTable.ajax.reload();
            }
        });
        request.done(function(msg){
            console.log(msg);
        });
        request.fail(function(jqXHR, textStatus){
            console.log("Request failed:" + textStatus)
        });
    }else{
        //Do Something 
    }
}); 
});

<?php
include ('db.php');
$query = '';
$output = array();
$query .= "SELECT branch.branch, outlet.outlet, itemuser.itemuser, 
items.item, items.brand, items.serialno, items.pdate, items.remarks FROM 
((items INNER JOIN itemuser ON items.uid = itemuser.uid)
           INNER JOIN outlet ON itemuser.oid = outlet.oid)
           INNER JOIN branch ON branch.bid = outlet.bid
           ";
$data = array();
if(isset($_POST["bid"])){

$query .= ' WHERE branch.bid = "'.$_POST["bid"].'" ORDER by outlet.oid, 
items.uid DESC '; 
}
else
{
$query .= ' ORDER by outlet.oid, items.uid DESC ';
}
$statement = $connection->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$data = array();
$filtered_rows = $statement->rowCount();
foreach($result as $row)
{
$sub_array = array();
$sub_array[] = $row["branch"];
$sub_array[] = $row["outlet"];
$sub_array[] = $row["itemuser"];
$sub_array[] = $row["item"];
$sub_array[] = $row["brand"];
$sub_array[] = $row["serialno"];
$sub_array[] = $row["pdate"];
$sub_array[] = $row["remarks"];
$data[] = $sub_array;
}
$output = array(
"draw"              =>  intval($_POST["draw"]),
"data"              =>  $data
);
echo json_encode($output);
/* Insert Function */
?>

如何根据ajax结果的post请求重绘我的数据表...它不会重新加载我的数据表

最佳答案

您可能正在寻找dataTable.ajax.reload()方法: https://datatables.net/reference/api/ajax.reload()

关于PHP PDO 使用下拉菜单过滤数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51784044/

相关文章:

mysql - 仅获取 2 个结果,每个结果对应具体字段的不同值

带有 MySQL 查询表的 PHP 函数

java - Volley 库在发布请求时给出错误 500

php - 如何根据用户输入从多个表的单个列中获取所有行?

php - 替换 php5 中已弃用的 register_globals 以更新 exec 使用的路径变量?

php - 在 Windows 上从 PHP 运行 Grunt 命令

javascript - 获取图像链接到网站

html - 如何使本身以百分比定义的父div的固定div宽度?

javascript - 为什么在 HTML 中使用 onClick() 是一种不好的做法?

mysql - 在 MySQL 中混合 MATCH AGAINST 与 JOINO