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());