That's a question many people ask themselves, and there is probably not one right answer...
What I generally do, in your case, is :
purchase.php
displays the form
- that form posts on itself (ie,
purchase.php
)
- when data has been submitted, it is dealt with -- still in
purchase.php
- if there is an error (like something not OK in the input), you can re-display the form really easily, this way : you already have every values that were typed in by the user
- if there is no error, you can do whatever you have to with the data ; like set it in session, if that's what you need, or save it to database, for instance.
- only when everything was OK (data validation OK and storage OK), you redirect to "
confirm.php
"
- that confirmation page does nothing except display a message saying "thanks for your purchase", or something like that.
It means putting more stuff in your purchase.php
, yes :
- (re-)displaying of the form
- dealing with the input
But, this way, it is really easier to re-display the form, pre-filled with what the use first typed, when there's a validation error.
You can use functions/classes/methods or even some included files, though, to not end up with one big chunk of un-readable / un-maintenable code...
If your form posts to another page, it'll be really harder to re-display the form... If you are using redirections, you'll to pass everything in the URL, and it'll be a mess (And there's a size limit, too)
Here, it means I would totaly remove your review-purchase.php
file ; and transform it to a confirmation page, so the user knows everything was OK and his purchase is being take care of.
I suppose it's quite what you meant in your last paragraph, actually :-)
Just beware : you have to think about escaping data before injecting it back into the form (see htmlspecialchars
and/or htmlentities
) ; that is true for everything you get from the user *(And, probably, for PHP_SELF too, I'd say)* ;-)