mysql 查询复选框,通过选中具有自动更新的多个复选框来查询MySQL

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.

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

Windows

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.

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

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

var id=$(this).attr('id');

$.get("review.php", {param: id},

function(data) {

$("div#update").html(data);

}

);

});

Review.php:

if (isset($_GET['param'])) {

$param = $_GET['param'];

}

$qry = mysql_query("SELECT * FROM reviews WHERE os = '".$param."' ") or die(mysql_error());

Thank you.

解决方案

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:

if (isset($_GET['param'])) {

$param = "('" . str_replace(",", "','", $_GET['param']) . "')";

}

$qry = mysql_query("SELECT * FROM reviews WHERE os IN $param") or die(mysql_error());

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值