doucong7963
2018-04-09 11:41
浏览 17
已采纳

使用laravel中的ajax更新购物车产品时出错

I want to update my shopping cart product using ajax.

It works only on first product not whole cart product and When i update the quantity value it not reflect on total price..

But when i refresh the page it reflect in total price..

shoppingcart_blade :

<input type="hidden" name="product_id" id="product_id<?php echo $count; ?>" value="{{ $cartproduct->id }}"/>

<input type="number" name="qty" min="1" max="20" id="updateQty<?php echo $count; ?>" value="{{ $cartproduct->qty }}" class="btn-qty-cart" autocomplete="off"/>
<input type="hidden" id="rowId<?php echo $count; ?>" value="{{ $cartproduct->rowId }}" name="rowId"/>  

<script type="text/javascript">

<?php for($i=1;$i<30;$i++){ ?>
$('#updateQty<?php echo $i; ?>').on('change keyup',function(){

    var qty = $('#updateQty<?php echo $i; ?>').val(); 
    var rowId = $('#rowId<?php echo $i; ?>').val(); 
    var product_id = $('#product_id<?php echo $i; ?>').val();

    if(qty<=0)
    {
        alert('Enter only valid Quantity');
    }
    else
    {
        $.ajax({ 
        type: "get", 
        url: "<?php echo url('shopping_cart/'); ?>/"+product_id, 
        data: { 
        'qty': qty, 
        'rowId': rowId,
        'product_id' :product_id, 
        }, 
        success: function(data) { 
        //alert(data); 
        //console.log(data); 
        } 
        });
    }

});
<?php } ?>
</script>  

Controller :

public function updateCartQuantity(Request $request,$id)
    {
        $product_id=$request->product_id;
        Cart::update($request->rowId,$request->qty);

        return redirect()->back();
    }
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题