无法使用 ajax、codeignier 获取输出购物车



我一直在努力使用ajax创建购物车。我是codeigniter和ajax的新手,但来自ajax的请求似乎是成功的,因为success:函数中的警报("success"(正在工作。但是我不能从控制器输出数据。在我想输出购物车的地方,我只收到了"欢迎来到示例窗口"。你能告诉我我做错了什么吗?

产品视图:

<?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>

我解决了这个问题。问题出现在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' 

它奏效了;

相关内容

  • 没有找到相关文章

最新更新