Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent script from re-executing action on page refresh

I am building my own php cart for the past week and I got stuck in some issues.

I managed to add new items in the cart, and the URL looks like below.

http://blah-blah.com/order/index.php?action=add&id=84

The question is simple: How can I prevent from adding the same item again in the cart if someone refreshes the page? Cause now every time someone refreshes the page, it changes the quantity to +1 for the specific item..

Also, after moving to the checkout page, if they press the back button in the browser, AGAIN the quantity will change to +1;

Any recommendations?

like image 933
yorgos Avatar asked Jun 07 '11 14:06

yorgos


1 Answers

Yea. Have them set, rather than increment the quantity.

Also, you'd usually use POST (not GET) requests for this sort of action. Browsers know this and cunningly ask the user whether they want to re-submit POST data.

The cleanest approach for your user may be to do the whole thing with AJAX. If they go "back", they'll just go back to the last page they visited without trouble. This would be equivalent to how comments on Stack Overflow are submitted: you cannot go "back" to your submission step and end up writing duplicate comments.

like image 118
Lightness Races in Orbit Avatar answered Sep 23 '22 02:09

Lightness Races in Orbit