duanpanzhu2910
duanpanzhu2910
2019-03-28 10:44

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

  • php
  • ajax
  • jquery

I am creating ecommerce site by php. In my home page when anyone click any category (like mobile) in shop page i wanna show those category wise product by getting the category id in shop page url. I wanna make this by using ajax and also when click any brand (like samsung) checkbox in shop page brand wise product will auto load by ajax.

I am able to show brand wise product when checkbox checked but i can't show category wise product because dont know how to pass category id by ajax and catch the id on shop page

I have tried this way

In shop page Ajax call

<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>

show category and brand wise product

<?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;
}

Since 6 month i am stuck on this project, if its solve then user side will be complete, So I humbly request for your help ....

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答