简体   繁体   English

当页面使用Ajax在Codeigniter中刷新时,将其插入购物车

[英]insertion into cart does when page refreshes in codeigniter with ajax

I am adding items into cart with ajax in codeigniter. 我正在使用Codeigniter中的Ajax将商品添加到购物车中。 My problem is that the cart got updated when i refreshes page. 我的问题是,刷新页面时购物车已更新。 I have ajaxify it to prevent page refresh. 我已经将其关闭以防止页面刷新。 but its not happening. 但它没有发生。 My code is right and there is no error. 我的代码正确,没有错误。 but still its not working. 但仍然无法正常工作。 my controller code is 我的控制器代码是

public function add_to_cart()
  {
    $item_id = $this->input->post('item_id');
    $item_name = $this->input->post('item_name');
    $item_price = $this->input->post('item_price');
    $data = array(

                       'id'      => rand(5,1000),
                       'qty'     => 1,
                       'price'   => $item_price,
                       'name'    => $item_name,


            );

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

  }

my view code is 我的查看代码是

    function insert()
{
var item_id=$("#item_id").val(); 
var item_name=$("#item_name").val();
var item_price=$("#item_price").val();
var dataString = "&item_id=" + item_id + "&item_name=" + item_name + "&item_price=" + item_price;
    $.ajax({  
        type: "POST",  
        url: "http://localhost/wah/cart_test/add_to_cart",  
        data: dataString,

        success: function()
        {
            alert('hello');
        }
    });
} 



   <form id="form">
                    <input type="hidden" id="item_id" name="item_id" value={{data.id}}> <input type="hidden" id="item_name" name="item_name" value={{data.item_name}}> <input type="hidden" id="item_price" name="item_price" value={{data.price}}>
                    <p><a href="#" onclick="insert()" class="btn btn-primary">Add to Cart</a></p>
                     </form>

the concept of the cart is to add the cart array in a session so the php will not feel the changes until you reload the page cart的概念是在会话中添加cart数组,因此php在重新加载页面之前不会感觉到更改

so you have to append the table with javascrip 所以你必须用javascrip追加表

// in controller     
public function add_to_cart()
      {
        $item_id = $this->input->post('item_id');
        $item_name = $this->input->post('item_name');
        $item_price = $this->input->post('item_price');
        $data = array(

                           'id'      => rand(5,1000),
                           'qty'     => 1,
                           'price'   => $item_price,
                           'name'    => $item_name,


                );

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

          }




 // in your javascript 
     $.ajax({  
            type: "POST",  
            url: "http://localhost/wah/cart_test/add_to_cart",  
            data: dataString,



    success: function(data)
        {

         // just replace YOUR-TABLE-ID with table id 
         //and complete the tr you want to append

          var tr = "<tr><td>"+data.id+"</td><td>"+data.name+"</td></tr>";
          $("#YOUR-TABLE-ID tr:last").after(tr);
        }
            });

What is happening when you try to use $item_id instead radnom number: 当您尝试使用$ item_id代替radnom number时会发生什么:

$data = array(
    'id'      => $item_id,
    'qty'     => 1,
    'price'   => $item_price,
    'name'    => $item_name,
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM