简体   繁体   中英

Add items to shopping cart AJAX Laravel

I'm using Laravel for ecommerce website and adding items by this way:

<div class="product-icon-container">
    <a href="{{ route('product.addToCart', ['id' => $product->id]) }}"><i class="fa fa-shopping-cart"></i></a>
</div>

Can you suggest AJAX solution to adding items to cart without refreshing page?

You can capture onclick and submit asynchronously with Jquery .

<script type="text/javascript">
        $('.product-icon-container').find('a').click(function (event){
            event.preventDefault();
            $.ajax({
                url: $(this).attr('href')
                ,success: function(response) {
                    alert(response)
                }
            });
            return false; //for good measure
        });
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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