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条回答 默认 最新

  • dqknycyt92288 2018-04-09 13:24
    关注

    If you want to call ajax for dynamic data and change any thing into database then you have to call ajax only one time and send the data dynamically.. In your case you are calling ajax in loop for 30 times, that not right way to call dynamic data..

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?