我正在通过php创建电子商务网站。在我的主页上,当有人单击商店页面中的任何类别(例如移动设备)时,我想通过在商店页面URL中获取类别ID来显示这些类别明智的产品。我想通过使用Ajax来做到这一点,而且当单击商店页面中的任何品牌(如三星)复选框时,品牌明智产品都会通过Ajax自动加载。
当我选中复选框时,我能够显示品牌明智的产品,但是我无法显示类别明智的产品,因为不知道如何通过ajax传递类别ID并在商店页面上捕获该ID
我已经尝试过这种方式
在商店页面Ajax呼叫
<script type="text/javascript">
$(document).ready(function() {
filter_data();
function filter_data()
{
$('.filter_data').html('<div id="loading"style=""></div>');
var action='action';
var brand=get_filter('brand');
var id = <?php echo $_GET['id']; ?>; // get id from page url
$.ajax({
url: 'action.php',
type: 'POST',
data: {action: action,brand: brand,id: id},
success:function(data)
{
$('.filter_data').html(data);
}
});
}
//load by check the checkbox
function get_filter(class_name) {
var filter=[];
$('.'+class_name+':checked').each(function() {
filter.push($(this).val());
});
return filter;
}
$('.common_selector').click(function() {
filter_data();
});
});
</script>
显示类别和品牌明智的产品
<?php
session_start();
include'core/db.php';
if (isset($_POST["action"])) {
$query="select * from product where cat=id "; // selecting category wise product by id which was sent from ajax but faild
}
if (isset($_POST["brand"])) {
$brand_filter = implode(',',$_POST['brand']);
$query="select * from product where brand IN ('$brand_filter')";
$dbquery=mysqli_query($db,$query);
$count =mysqli_num_rows($dbquery);
$output = '';
if ($count > 0) {
while ($bf=mysqli_fetch_assoc($dbquery))
{
$output.='<div class="col-md-3 ">
<div class="product">
<h3>'.$bf["title"].'</h3>
<img src="img/'. $bf['image'] .'" class="img-responsive" >
<p class="Price"><b>Price:</b>'.$bf["price"].' </p>
<a href="details.php?id='.$bf['id'].'" class="btn btn-success">
Details
</a>
<button id="product" pid='.$bf["id"].' class="btn btn-primary">
<span class="glyphicon glyphicon-shopping-cart"></span> Add Cart
</button>
</div>
</div>';
}
} else {
$output = '<h3>No Data Found</h3>';
}
echo $output;
}
自6个月以来,我一直停留在该项目上,如果解决了,那么用户端将是完整的,因此,我很谦虚地请求您的帮助....
答案 0 :(得分:0)
您必须先获取id的值,然后再将其传递给查询
<?php
session_start();
include'core/db.php';
if (isset($_POST["action"])) {
$_SESSION['id'] = $_POST['id'];
$id=$_SESSION['id'];
$query="select * from product where cat='$id' "; // selecting category wise product by id which was sent from ajax but faild
}
elseif (isset($_POST["brand"])) {
$brand_filter = implode(',',$_POST['brand']);
$query="select * from product where brand IN ('$brand_filter')";
}
$dbquery=mysqli_query($db,$query);
$count =mysqli_num_rows($dbquery);
$output = '';
if ($count > 0) {
while ($bf=mysqli_fetch_assoc($dbquery))
{
$output.='<div class="col-md-3 ">
<div class="product">
<h3>'.$bf["title"].'</h3>
<img src="img/'. $bf['image'] .'" class="img-responsive" >
<p class="Price"><b>Price:</b>'.$bf["price"].' </p>
<a href="details.php?id='.$bf['id'].'" class="btn btn-success">
Details
</a>
<button id="product" pid='.$bf["id"].' class="btn btn-primary">
<span class="glyphicon glyphicon-shopping-cart"></span> Add Cart
</button>
</div>
</div>';
}
} else {
$output = '<h3>No Data Found</h3>';
}
echo $output;