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 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥50 安装华大九天aether
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)