Add products without refresh

Asked

Viewed 299 times

-3

Good morning, when adding a product to your cart every time you update your browser it increases the product by +1. How to prevent this? This is how to add it:

    //adiciona produto

    if(isset($_GET['acao'])){
      $id = intval($_GET['id']);
          if(!isset($_SESSION['shop'][$id])){
             $_SESSION['shop'][$id] = 1;
          }else{
             $_SESSION['shop'][$id] += 1;
          }
       }
  • I would recommend you use Ajax to add in the cart, so you would have no problem with arguments passed in the url.

  • You are talking about the same product being added multiple times or the quantity of the product ?

  • You can add both the product and quantity via AJAX, the difference is that it will increment whenever the user clicks a button and not when the page is loaded. In the PHP file you check if the product is in the session, if yes increments, if not adds.

  • After adding you can redirect it to the cart page without passing the action parameter, for example: header('Location: carrinho.php');, ai when the user refreshes the page, will not enter if, not adding an amount to the cart, or else, use ajax...

  • 3

    Possible duplicate of Refresh-free browsing!

  • 2

    I don’t understand the duplicate vows. What does Ajax have to do with it? You want the person to do it in ajax just to avoid the problem?

  • @Bacco is not that crazy, but perhaps because the question lacks explanations and, besides, the title "No refresh" on the site has a lot of subject on it. Of course you can solve the situation without using ajax to solve the problem, but "without refresh", the solution I would give would be yes using AJAX. If the problem is another, then the title and description of the question must be another.

  • Clayton, you are wearing session_start()?

Show 3 more comments

1 answer

-2

!isset($_SESSION['shop'][$id])

Try replacing isset with Empty, like this:

!empty($_SESSION['shop'][$id])

You can also use php functions to manipulate array like in_array, array_key_exists and etc... Maybe it will solve.

  • The solution I found was to block F5 with: <body onkeydown="Return (Event.keycode == 154)">

  • @Claytoncampos this is not a solution, it is a patch that does not solve the problem. Even a simple redirect is better than that.

  • @Claytoncampos better wait for more alternatives, to replace key detection with something more robust. I think I’ve seen some post here dealing with this problem, if I find I put the link in the question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.