1

So, the problem is:

When I press "purchase" button, it changes color and link for it and adds product to cart:

$(document).ready(function(c) {
    $('.item_add').click(function (event) {
    var addButton = $(this).children('p');
    if(addButton.hasClass('added'))
    {
        window.location.href="http://site.loc/cart";
    }

    else
    {
        event.preventDefault();
        var id = $(this).attr('id');
        var href = $(this).attr('href');
        var data = {'cart_item_id': id};
        var carturl = "http://site.loc/cart";

            $.ajax({
                url: href,
                data: data,
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                type: 'POST',
                dataType: 'JSON',
                success: function (html) {
                        addButton.addClass('added');
                        addButton.css('background', 'green');
                        addButton.parent('a').attr('href', carturl);
                        $('#addOrAlready').text('Added');
                }
            });
    }
});

Then, when i click this button, it gets me to cart, as supposed, BUT, when i press "back" button in browser, the page is same: button is not green, and link is not updated to /cart, data updates only when i RELOAD that page. Why data doesn't update when i go to cart? How to update it?

View:

<a href="{{$cart_items->contains('id',$productItem->id) ? route('IndexCart'): route('AddToCart')}}" class="item_add" id="{{$productItem->id}}">
    <p class="number item_price {{$cart_items->contains('id',$productItem->id) ? 'added': ''}}">
        <i> </i>${{$productItem->price}}
    </p>
</a>
Wang Liang
  • 4,244
  • 6
  • 22
  • 45
Batmannn
  • 237
  • 4
  • 12

1 Answers1

0

The back button doesn't necessarily update the dom, It must be something related to this question Reload the site when reached via browsers back button

Community
  • 1
  • 1
Sérgio Reis
  • 2,483
  • 2
  • 19
  • 32