如何通过ajax(Jquery)将多个复选框的值传递给mysql,我的意思是,最初我不知道会选择多少个复选框。
例如,我必须根据所选颜色(红色,绿色)从数据库中选择汽车,但是在我决定选择黑色之后。网页应该显示红色,绿色和黑色汽车的结果。我怎么能这样做?
任何想法?
我的代码(但它只能选择一个项目)
<form>
<div class="checkbox" >
<label><input type="checkbox" value="1" class="item">Red</label>
<label><input type="checkbox" value="2" class="item">Green</label>
<label><input type="checkbox" value="4" class="item">Black</label>
</div>
</form>
<script>
$(document).ready(function () {
$('.item').change(function(){
if($(this).is(':checked')){
var name = $(this).val();
$.post('load.php', {name:name}, function(data){
$('#name-data').html(data);
});
}
});
});
</script>
带有mysql查询的php文件
<?php
//1.Create a db connection
define("DB_SERVER", "localhost");
define("DB_USER", "pavel");
define("DB_PASS", "2601532");
define("DB_NAME", "testik");
$connection = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
//Test if connection succed
if(mysqli_connect_errno())
{
die("Database connection failed: ".
mysqli_connect_error().
"(" . mysqli_connect_errno(). ")"
);
}
$Query = "SELECT * FROM items WHERE id ='".trim($_POST['name'])."'";
$QueryResult = mysqli_query($connection , $Query);
$QueryRow = mysqli_fetch_assoc($QueryResult);
?>
<div>
<p><?php echo $QueryRow['cost']; ?></p>
<img src="<?php echo $QueryRow['img']; ?>">
</div>