通过使用自动更新检查多个复选框来查询MySQL

时间:2021-02-18 00:04:50

I would like to build a kind of advanced mySQL search - with the help of JQuery - where the user query the database by checking checkboxes and as a checkbox is checked a DIV is immediately updated via AJAX with the results. A kind of autocomplete but with checkboxes.

我想在JQuery的帮助下构建一种高级的mySQL搜索 - 用户通过选中复选框来查询数据库,并且当复选框被选中时,DIV会立即通过AJAX更新结果。一种自动完成功能,但带有复选框。

For example, I have two or more checkboxes as these:

例如,我有两个或更多复选框,如下所示:

<input type="checkbox" id="Windows" name="system"> Windows
<input type="checkbox" id="Linux" name="system"> Linux

If the user checks "Windows" mySQL does immediately (without clicking any Submit button) return a list of Windows softwares. If the user checks both, mySQL should immediately return a list of Windows AND Linux softwares.

如果用户检查“Windows”mySQL立即执行(不单击任何“提交”按钮),则返回Windows软件列表。如果用户同时检查两者,mySQL应立即返回Windows和Linux软件列表。

The following works fine for one choice but not for both:

以下工作正常,但不适用于两者:

<div id="update"></div>

$('input[name=system]').click(function(){
    var id=$(this).attr('id');
    $.get("review.php", {param: id}, 
        function(data) {
            $("div#update").html(data);
        }
    );
});

Review.php:

Review.php:

if (isset($_GET['param'])) {
    $param = $_GET['param'];
}
$qry = mysql_query("SELECT * FROM reviews WHERE os = '".$param."' ") or die(mysql_error());

Thank you.

谢谢。

2 个解决方案

#1


1  

Try this:

尝试这个:

$('input[name=system]').click(function(){

    var ids = [];
    $('input[name=system]:checked').each(function() {
        ids.push($(this).attr('id'));
    });
    ids = ids.join(",");

    $.get("review.php", {param: ids}, 
        function(data) {
            $("div#update").html(data);
        }
    );
});

And in your PHP:

在你的PHP中:

if (isset($_GET['param'])) {
    $param = "('" . str_replace(",", "','", $_GET['param']) . "')";
}
$qry = mysql_query("SELECT * FROM reviews WHERE os IN $param") or die(mysql_error());

#2


0  

You need to create one click function for both check-boxes which would check both (all) values and create one http request such as ?param=windows,linux.

您需要为两个复选框创建一个单击函数,它将检查两个(所有)值并创建一个http请求,例如?param = windows,linux。

You also need to update condition in mysql query, so it'd look like:

你还需要在mysql查询中更新条件,所以它看起来像:

$params = explode( ',', $param);
// escape all params (mysql_real_escape_string)
$condition = 'os IN("' . implode( '", "', $params) . '")';
$qry = mysql_query("SELECT * FROM reviews WHERE $condition") or die(mysql_error());

#1


1  

Try this:

尝试这个:

$('input[name=system]').click(function(){

    var ids = [];
    $('input[name=system]:checked').each(function() {
        ids.push($(this).attr('id'));
    });
    ids = ids.join(",");

    $.get("review.php", {param: ids}, 
        function(data) {
            $("div#update").html(data);
        }
    );
});

And in your PHP:

在你的PHP中:

if (isset($_GET['param'])) {
    $param = "('" . str_replace(",", "','", $_GET['param']) . "')";
}
$qry = mysql_query("SELECT * FROM reviews WHERE os IN $param") or die(mysql_error());

#2


0  

You need to create one click function for both check-boxes which would check both (all) values and create one http request such as ?param=windows,linux.

您需要为两个复选框创建一个单击函数,它将检查两个(所有)值并创建一个http请求,例如?param = windows,linux。

You also need to update condition in mysql query, so it'd look like:

你还需要在mysql查询中更新条件,所以它看起来像:

$params = explode( ',', $param);
// escape all params (mysql_real_escape_string)
$condition = 'os IN("' . implode( '", "', $params) . '")';
$qry = mysql_query("SELECT * FROM reviews WHERE $condition") or die(mysql_error());