duanpanzhu2910 2019-03-28 10:44
浏览 522
已采纳

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

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条回答 默认 最新

  • dpd20130 2019-03-28 10:54
    关注

    You have to get the values of id before passing it in your query

    <?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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 AT89C52单片机C语言关于串口通信的位操作
  • ¥15 电脑出问题了,说是usbmom注册表没
  • ¥20 需要步骤截图(标签-服务器|关键词-map)
  • ¥50 gki vendor hook
  • ¥15 灰狼算法和蚁群算法如何结合
  • ¥15 这是一个利用ESP32自带按键和LED控制的录像代码,编译过程出现问题,请解决并且指出错误,指导如何处理 ,协助完成代码并上传代码
  • ¥20 stm32f103,hal库 hal_usart_receive函数接收不到数据。
  • ¥20 求结果和代码,sas利用OPTEX程序和D-efficiency生成正交集
  • ¥50 adb连接不到手机是怎么回事?
  • ¥20 抓取数据时发生错误: get_mooncake_data() missing 1 required positional argument: 'driver'的问题,怎么改出正确的爬虫代码?