【问题标题】:Can't get output cart using ajax, codeignier无法使用 ajax、codeigniter 获取输出购物车
【发布时间】:2020-04-17 09:20:31
【问题描述】:

我一直在努力使用 ajax 创建购物车。我是 codeigniter 和 ajax 的新手,但似乎来自 ajax 的请求是成功的,因为 success:function 中的 alert('success') 正在工作。但我无法从控制器输出数据。在我想输出购物车的地方,我只是得到“欢迎来到 xamp 窗口”你能告诉我我做错了什么吗?

产品视图:

            <?php if(count($data)): ?>
                <?php foreach ($data as $item) { ?>
                    <div class="shop-item">
                        <div class="image">
                        <a href="<?php echo base_url(); ?>index.php/items/item1">
                                <img class="shop-item-image" src="<?php echo base_url("uploads/".$item->image) ?>" alt="Ariana Grande: thank u, next exclusive clear/pink lp"/>
                            </a>
                            <?php if($item->status=='sold'): ?>
                            <p class="sold-btn">Sold out</p>
                            <?php  else:  ?>
                            <button class="cart-btn" data-productid="<?php echo $item->id;?>" data-productartist="<?php echo $item->artist;?>"
                                    data-producttitle="<?php echo $item->title;?>" data-productprice="<?php echo $item->price;?>" data-productimage="<?php echo $item->image;?>">
                                <i class="fa fa-shopping-cart"></i>Add to cart</button>
                            <?php endif; ?>
                        </div>

                        <a class="artist" href="#">
                            <div class="shop-item-details">
                                <span class="brand"><?php echo $item->artist; ?></span>
                            </div>
                        </a>

                            <div class="title">
                                <span class="format double-vinyl-lp"><?php echo $item->title; ?></span>
                            </div>
                            <div>
                                <span class="price">€<?php echo $item->price; ?></span>
                            </div>

                    </div>


                <?php } ?>
            <?php endif; ?>
        </div>

jQuery

$(document).ready(function() {
    $('.cart-btn').click(function () {
            var product_id = $(this).data("productid");
            var product_artist = $(this).data("productartist");
            var product_title = $(this).data("producttitle");
            var product_price = $(this).data("productprice");
            var image = $(this).data("productimage");
            var quantity = 1;




            $.ajax({
                url :'/index.php/cart/add_to_cart',
                method : "POST",
                data : {product_id: product_id, product_artist: product_artist, product_title: product_title, product_price: product_price, quantity:quantity, image:image},
                success: function(data){
                    alert(product_artist);

                    $('#detail-cart').html(data);
                },


            });
        });

        $('#detail-cart').load("<?php echo site_url('index.php/cart/load_cart');?>");



    })

控制器

public function add_to_cart() {

        $data = array(
            'id' => $this->input->post('product_id'),
            'name' => $this->input->post('product_artist'),
            'price' => $this->input->post('product_price'),
            'qty' =>1
            //'title'=>$this->input->$_POST('product_title'),
            //'image'=>$this->input->$_POST('image')
        );
        $this->cart->insert($data);

            print_r(($this->cart->total_items()));
        echo $this->show_cart();
    }

     public function show_cart(){
        $output = '';
        $no = 0;
        foreach ($this->cart->contents() as $items) {
            $no++;
            $output .='
                        <div>
                            <a href="">

                                <p>hello</p>
                        </a>

                        </div>

                            <div class="description">
                                <a class="artist" href="">'.$items['name'].'</a>

                                    <p class="mini-total"><span>'.number_format($items['qty']).'</span>&times;<span>'.number_format($this->cart->total()).'</span></p>
                                <a href="#">remove</a>
                            </div>
                                <div class="quantity">
                                    <i class="fa fa-chevron-up"></i>
                                    <i class="fa fa-chevron-down"></i>
                                </div>

            ';

        }


        return $output;
    }
    public function load_cart(){
        echo $this->show_cart();
    }

}

证书视图我想在不同的视图中显示它作为标题的一部分

<div id="detail-cart" class="mini-cart-item">

                                        </div>

【问题讨论】:

  • 你在 ajax 成功中得到了正确的响应吗?

标签: php jquery codeigniter shopping-cart


【解决方案1】:

我发现了问题。问题出在 url 中

url :'/index.php/cart/add_to_cart' 

没用;都不是:

url :'<?phph echo base_url();?>index.php/cart/add_to_cart

我所做的是声明:

<script> 
    base_url= '<?phph echo base_url();?>index.php/cart/add_to_cart'
   </script>

然后将url设置为

   url: base_url+'index.php/cart/add_to_cart' 

它成功了;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-26
    相关资源
    最近更新 更多