Невозможно получить выходную корзину, используя ajax, кодировщик - PullRequest
0 голосов
/ 17 апреля 2020

Я изо всех сил пытался создать корзину для покупок, используя ajax. Я новичок в codeigniter и ajax, но кажется, что запрос от ajax успешен, так как предупреждение ('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>

1 Ответ

0 голосов
/ 22 апреля 2020

Я понял проблему. проблема была в 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' 

, и это сработало;

...