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 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝