【问题标题】:Shopping Cart Issue in CodeigniterCodeigniter 中的购物车问题
【发布时间】: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


【解决方案1】:

PHP 脚本,即:&lt;?php echo base_url();?&gt; 不会在.js 文件中执行。因此,您的 url 如何在 cmets 中逐字显示在开发工具中。

您可以将 js 文件的内容移动到您的标头模板中,以便它驻留在视图中。或者您可以在 js 中自己定义 base_url() 为 var (var base_url = 'http://somesite.com/';) 并像 base_url 一样使用它或执行以下操作:

url:"/cart/insert_product" 假设 cart 不在 controllers 的子目录中

【讨论】:

  • 你能指导我完成图像处理吗.. 我正在尝试显示带有项目的图像,但它不起作用。
  • 链接我堆栈上的问题,我看看。 (如果您还没有提出问题,请提出新问题)
【解决方案2】:

你为什么在 Controller 中使用 $data。 如果我没记错,那么您是在单独发送值,而不是在“$data 多关联数组”中

$data=array(
              array(
                'id'      => $_POST['product_id'],
                'qty'     => $_POST['quantity'],
                'price'   => $_POST['product_price'],
                'name'    => $_POST['product_name'])
        );

尝试删除 $data=array() 并使用以下代码:

 $id = $this->input->post('product_id');
        $quantity = $this->input->post('quantity');
       $product_price = $this->input->post('product_price');
        $product_name = $this->input->post('product_name');

【讨论】:

【解决方案3】:

我一般都是这样获取post数据并插入到表中的:

 $productId = $this->security->xss_clean($this->input->post('product_id'));
 $quantity = $this->security->xss_clean($this->input->post('quantity'));
 $productPrice = $this->security->xss_clean($this->input->post('product_price'));
 $productName = $this->security->xss_clean($this->input->post('product_name'));


    $data = array(
       "id" => $productId,
       "qty" => $quantity,
       "price" => $productPrice,
       "name" => $productName
    );

    $this->cart->insert($data);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-17
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多