如何通过ajax将id从url参数传递到另一个页面

问题描述 投票:0回答:1

我正在通过php创建电子商务网站。在我的主页,当任何人点击商店页面中的任何类别(如移动)时,我想通过获取商店页面网址中的类别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个月开始我就被困在这个项目上,如果它解决了,那么用户方将完成,所以我谦卑地请求你的帮助....

php jquery ajax
1个回答
0
投票

您必须在查询中传递id之前获取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;
© www.soinside.com 2019 - 2024. All rights reserved.