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?
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.