Add to cart with Ajax without updating page - Laravel

0

Hello, I'm trying to use Ajax in a form to add products to the cart without refreshing the page but I can not if someone can help me.

I'm using Laravel 5.3 based on this site :

HTML

<form id="form_ideal" action="/cart/add" method="post" name="add_to_cart">
 {!! csrf_field() !!}
<input type="hidden" name="product" value="{{$product->id}}" />
<input type="hidden" name="qty" value="1" />
<span class="compr-games" title="Add to Cart"><button class="btn-add-to-cart"> Buy Now </button></span>
</form>

Script I'm trying:

<script type="text/javascript">
        $('#form_ideal').on('submit', function(e) {
            e.preventDefault(); 
            $.ajax({
                type: "POST",
                url: '/cart/add',
                data: $(this).serialize(),
                success: function(msg) {
                swal('Test')
                }
            });
        });
    </script>

What happens is that clicking the add button to the cart shows the success message but does not update the quantity in the menu, it gets "0", it only refreshes after I give F5 on the page,

I'm sorry if I've done something wrong but I started to mess with Laravel, ajax, those things ..

Thank you

    
asked by anonymous 11.09.2017 / 22:42

1 answer

0

If you update and appear that you have already implemented this function, then just pass this amount to client , to update cart with method html of J-query , example ...

$retorno = array('quantidade' => $quantidadeNoCarrinho, 'msg' => 'sucesso!');
echo json_encode($retorno);
exit();

in success take this amount

success: function(retorno) {
                alert(retorno.msg);
                $('.quantidade').html(retorno.quantidade)
                }
    
11.09.2017 / 23:03