2013-05-16 223 views
0

朋友们,我正在开发一个电子商务网站(使用Codeigniter)。在购物车上添加产品

在客户填写产品数量并点击“购买”后,只有带有“数量”字段的产品才会被装入购物车。你可以帮我吗?

HTML:

<form> 
<ul class="products-list"> 
    <li class="product"> 
     <input type="hidden" class="product_id" value="1"> 
     <span class="product-name">Product 1</span> 
     <span class="product-value">$ 100</span> 
     <span>Qnt. <input type="text" class="quantity"></span> 
    </li> 
     <li class="product"> 
     <input type="hidden" class="product_id" value="2"> 
     <span class="product-name">Product 2</span> 
     <span class="product-value">$ 100</span> 
     <span>Qnt. <input type="text" class="quantity"></span> 
    </li> 
</ul> 
<button>Buy</button> 
</form> 


的jQuery:

$.ajax({ 
    url: '/cart/add/'+product_id+'/'+quantity, 
    type: 'GET', 
    success: function(data){ 
     if (data != false) { 
      window.location.reload(); 
     } 
    } 
}); 


我的PHP代码:

public function addItem($product_id, $quantity){ 
    $product = $this->Produto_model->getById($product_id); 
    $this->carrinho->setItem($product, $quantity); 
    echo json_encode($this->carrinho); 
} 
+0

什么是你的问题?什么工作,什么不工作?它出错了哪里? – RST

回答

0

尝试这样

HTML:

<form id="form-cart"> 
    <ul class="products-list"> 
     <li class="product"> 
      <input type="hidden" class="product_id" value="1"> 
      <span class="product-name">Product 1</span> 
      <span class="product-value">$ 100</span> 
      <span>Qnt. <input type="text" class="quantity" value="1"></span> 
     </li> 
      <li class="product"> 
      <input type="hidden" class="product_id" value="2"> 
      <span class="product-name">Product 2</span> 
      <span class="product-value">$ 100</span> 
      <span>Qnt. <input type="text" class="quantity" value="1"></span> 
     </li> 
    </ul> 
    <button id="form-cart-btn">Buy</button> 
</form> 


<script type="text/javascript"> 
    $("#form-cart #form-cart-btn").click(function(){ 
     form = $("#form-cart"); 

     items = []; 

     form.find('li').each(function(i, e){ 
      _e = $(e); 
      qty = parseInt(_e.find('.quantity').val()); 

      if(isNaN(parseFloat(qty)) || ! isFinite(qty) || qty == 0) 
       return; 

      id = _e.find('.product_id').val(); 
      items.push('item[' + id + ']=' + qty); 
     }); 

     items = items.join('&'); 

     $.ajax({ 
      url: '/cart/add/?' + items, 
      dataType: 'json', 
      success: function(response) 
      { 
       if(response.status != 'success') 
        return false; 

       window.location.reload(); 
      } 
     }); 
     return false; 
    }); 
</script> 

车控制器:

class Cart extends CI_Controller 
{ 
    public function add() 
    { 
     $items = isset($_GET['items']) ? $_GET['items'] : array(); 

     foreach($items as $product_id => $qty) 
     { 
      $product = $this->Produto_model->getById($product_id); 
      if(! $product) 
      { 
       ++$i; 
       continue; 
      } 
      $this->carrinho->setItem($product, $qty); 
      ++$i; 
     } 

     header("Content-type: application/json"); 
     echo json_encode(array('status'=>'success')); 
     exit(); 
    } 
} 
+0

好的,我在routes.php文件中做了什么? 目前看起来像这样: $ route ['cart/add /(:num)/(:num)'] =“front/products/addItem/$ 1/$ 2”; –

+0

'$ route ['cart/add /'] =“front/products/addItem /”' – lysenkobv

相关问题