using header(location to avoid resubmission of form data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sparrrow
Forum Commoner
Posts: 81
Joined: Mon Oct 20, 2008 12:22 pm

using header(location to avoid resubmission of form data

Post by sparrrow »

So I have a shopping cart with all code contained within the one page. It has functions for shopping cart actions of add, update, delete that update the db table based on the user action. The type of scenario I'm trying to tackle is if a user adds something to their cart then refreshes, it adds the item again. etc etc typical form resubmission issue.

So I had an idea, tested it, and it appears to work. But I'm concerned that I haven't seen this solution on these boards or google anywhere so maybe there's a problem that I'm not seeing that could occur by doing it this way.

Basically, user submits change to shopping cart -> Shopping cart add, delete, or update function is called. At the end of each function, I just added header('Location: cart.php'); So page loads, cart is updated, then header location is changed to cart.php with no POST data, so user can freely refresh, use back button, whatever they want. What am I overlooking? This seems TOO easy...
User avatar
veridicus
Forum Commoner
Posts: 86
Joined: Fri Feb 23, 2007 9:16 am

Re: using header(location to avoid resubmission of form data

Post by veridicus »

That's a great solution. On some sites I do a redirect to a list view page after a create / edit / delete from a record's detail page. It solves the refresh/repost problem and often helps with usability.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: using header(location to avoid resubmission of form data

Post by Jade »

I've seen that done a lot with shopping carts. Especially with payment pages because you don't want someone paying twice -- although they ALWAYS find a way :banghead:
Post Reply