根据复选框选择

时间:2018-02-12 13:47:03

标签: php jquery mysql datatables

我有一个页面,我在其中显示MySQL数据库的一组结果并将其显示在数据表中。我希望能够通过复选框选择不同的选项,并相应地更新结果。

我创建了复选框

<input type="checkbox" name="filter_products" class='form-control filter_products' id='show_zeros' value="Zeroes"> Show Zeroes<br>
<input type="checkbox" name="filter_products" id='show_all' value="All"> Show all products<br>

然后我将包含一个单独的php文件,该文件运行MySQL查询并根据结果创建表

include ('load_table.php');

我尝试过使用AJAX调用

$('input').on('ifClicked', function(event)
    {
        if ($('#show_zeros').prop('checked'))
            var show_zeros = $('#show_zeros').val();

        if ($('#show_all').prop('checked'))
            var show_all = $('#show_all').val();

        $.ajax({ 
            url: "load_table.php",
            data: {'show_zeros' : show_zeros, 'show_all' : show_all},
            type: 'post',
            success: function(result) {

            },
                error: function() {}

            }); 
    });

但我似乎没有得到任何结果。事实上,当我尝试

时,它看起来并不像show_zeros和show_all变量那样
$show_zeroes = $_POST['show_zeros'];
$show_all = $_POST['show_all'];
echo 'Show zeroes: ' . $show_zeroes;
echo 'Show All: ' . $show_all;

我得到空字符串。

在代码中,我有以下代码来操作MySQL的查询。

if ($show_zeroes == 'Zeroes' && $show_all == 'undefined') {
  $query = "select sp.product_id AS product_id, sp.department_id AS department_id, 
              sp.product_name AS product_name, sd.department_name AS department_name, 
              sp.unit_cost AS unit_cost, SUM(sp.quantity_units) AS quantity_counted, 
            ROUND(SUM(sp.quantity_units) * sp.unit_cost,2) AS total_cost 
            FROM stocktake_details sp 
              INNER JOIN store_departments sd 
              ON sd.id = sp.department_id 
            WHERE sp.stocktake_id = $stocktake_id 
            GROUP BY sp.product_id ORDER BY total_cost DESC";
} else if ($show_zeroes == 'Zeroes' && $show_all == 'All') {
  $query = "SELECT sp.product_id AS product_id, sp.department_id AS department_id, 
              sp.product_name AS product_name, sd.department_name AS department_name, 
              sp.unit_cost AS unit_cost, SUM(sp.quantity_units) AS quantity_counted, 
            ROUND(SUM(sp.quantity_units) * sp.unit_cost,2) AS total_cost 
            FROM stocktake_details sp 
              INNER JOIN store_departments sd 
              ON sd.id = sp.department_id 
            WHERE sp.stocktake_id = $stocktake_id 
            GROUP BY sp.product_id ORDER BY total_cost DESC";
} else {
  $query = "SELECT sp.product_id AS product_id, sp.department_id AS department_id, 
              sp.product_name AS product_name, sd.department_name AS department_name, 
              sp.unit_cost AS unit_cost, SUM(sp.quantity_units) AS quantity_counted, 
            ROUND(SUM(sp.quantity_units) * sp.unit_cost,2) AS total_cost 
            FROM stocktake_details sp 
              INNER JOIN store_departments sd 
              ON sd.id = sp.department_id 
            WHERE sp.stocktake_id = $stocktake_id AND sp.quantity_units > 0 
            GROUP BY sp.product_id ORDER BY total_cost DESC"; }

我是否正在努力实现我想要达到的目标,如果是这样,我哪里出错了?

谢谢。

0 个答案:

没有答案