【发布时间】:2018-03-08 03:08:51
【问题描述】:
我正在尝试在 CodeIgniter 中创建一个购物车,我尝试了文档中提到的方法,并且效果很好。但是当我尝试在模板中使用它时它不起作用。在文档中,产品是硬编码的,但我从数据库中获取产品并使用 ajax 将其插入。
我的插入代码 JS
$(document).ready(function(){
$('.add-to-cart').click(function(){
var product_id=$(this).data("productid");
var product_name=$(this).data("productname");
var product_price=$(this).data("productprice");
var quantity=$('#' + product_id).val();
if(quantity !='' && quantity>0)
{
$.ajax({
url:"<?php echo base_url();?>Cart/insert_product",
method:"POST",
data:{
product_id:product_id,product_name:product_name,product_price:product_price,quantity:quantity
},
success:function(data)
{
alert("Product added into cart");
// alert("Product added into cart and product info is: ID:" + product_id +'\n\r name:' + product_name + '\n\r price:' + product_price + '\n\r quantity:' + quantity);
// $('#' + product_id).val('');
}
});
}
else
{
alert("Please enter quantity");
}
});
});
我使用警报对其进行测试,以确保当我单击“添加到购物车”按钮时产品正在挑选。
我的控制器 Insert_Product
// my cart
public function insert_product()
{
$data=array(
'id' => $_POST['product_id'],
'qty' => $_POST['quantity'],
'price' => $_POST['product_price'],
'name' => $_POST['product_name']
);
// Inesrting Items into Cart
$this->cart->insert($data);
}
当我点击“添加到购物车”按钮时,它会显示成功消息,但我很确定产品没有插入购物车...
我尝试访问产品的位置。
<!-- cart items -->
<div class="nav-cart-items">
<?php $i = 1;
if(!$this->cart->contents()){echo "no items found in cart";}
foreach ($this->cart->contents() as $items):
var_dump($items);
if(empty($items)){echo "cart is empty";}else{ echo "<h1>cart has data</h1>";?>
<div class="nav-cart-item clearfix">
<h4>Cart Details</h4>
<div class="nav-cart-img">
<a href="#">
<img src="cart_small_1.jpg" alt="">
</a>
</div>
<div class="nav-cart-title">
<a href="#">
<?php echo $items['name'];?>
</a>
<div class="nav-cart-price">
<span><?php echo $items['qty'];?> x</span>
<span><?php echo $this->cart->format_number($items['price']);?></span>
</div>
</div>
<div class="nav-cart-remove">
<a href="#"><i class="ui-close"></i></a>
</div>
</div>
<?php $i++; ?>
<?php } endforeach;?>
</div> <!-- end cart items -->
如果购物车中没有商品,我将回显消息"no items found in cart",它向我显示了这一点。我也试试这段代码
<?php
ob_start();
var_dump($this->cart->contents());
$result = ob_get_contents();
?>
测试内容并显示输出:
F:\xampp\htdocs\cart\application\views\single_item.php:3:
array (size=0)
empty
至于我认为我做错了什么,但不知道在哪里...... 还请在 CodeIgniter 中建议一个购物车解决方案,因为在文档中购物车库已被贬值。
对此的任何帮助将不胜感激。
【问题讨论】:
-
print_r($data);在 insert_product() 中的数组之后...当您通过 ajax 添加项目时,该数组是否有数据? (应该可以在您的网络预览标签中看到)
-
除了
Request URL:http://localhost/cart/product/5 Request Method:GET Status Code:200 OK Remote Address:[::1]:80 Referrer Policy:no-referrer-when-downgrade Cache-Control:no-store, no-cache, must-revalidate Connection:Keep-Alive Content-Type:text/html; charset=UTF-8 Date:Thu, 08 Mar 2018 03:55:30 GMT Expires:Thu, 19 Nov 1981 08:52:00 GMT Keep-Alive:timeout=5, max=100,我在网络标签中看不到任何有关产品的数据信息 -
打开开发工具 (chrome) > 网络选项卡。通过将商品添加到购物车来提交 ajax 请求,您应该会在列表末尾看到名称为
insert_product的商品。单击它,然后应该显示更多选项卡。一个会有响应数据,点击那个,你应该看到你的数组。这是ajax调试101。 -
数据现在显示,但 url 似乎不正确......它的显示就像。
Request URL:http://localhost/cart/product/%3C?php%20echo%20base_url();?%3ECart/insert_product -
你没有看到在 js 中运行(或者说不运行)php 脚本有问题吗? ;)
标签: php ajax codeigniter codeigniter-3 shopping-cart