PHP Multiform Validation and Redirection
-
09-06-2019 - |
Question
I have buy.php
with a form where you enter items, quantity, shipping data, etc.
When you click the Submit button, it posts back to buy.php
($_SERVER['PHP_SELF'])
and does some data validation.
If there are fields missing or errors, they are highlighted. If everything is correct, I save the $_POST
data in $_SESSION
variables, then do a header('Location: check.php')
, where I display the data so the buyer can check the info one last time before actually buying.
Now, if I'm in check.php
and hit the Back button to buy.php
so I can change stuff, the browser asks if I want to resend the POST data. I'm trying to avoid that.
Anyone have any good advice or good practices for PHP Multiform validation?
Also, if I had n pages for the user to fill, buy.php, buy2.php, ... buyn.php
before check.php would the same ideas still hold?
Solution
You could do a redirect to buy.php after saving to the session object, which then does a server redirect to check.php, it would mean when the user clicks back, they're going back to the GET request not the POST request
OTHER TIPS
Yes - I agree with above. I ALWAYS do a redir
away from the last post, so clicking back bounces them back without that error OR re-submissions. it also avoids complications. u can always tag the redir
link page with a ?m
or &m
(i.e.: page.php?m
) and have this at top of page: (use elseif
there after)
if (isset($_GET['m'])) {
echo 'order placed.';
}
else {
//...
}
You can have it all on one page too. Just name the submit buttons submit1
, submit2
, like: (bear in mind if you use an image for submits, it becomes $_POST['submit1_x']
:)
if (isset($_POST[submit1]) {
//validate + save session data from form1
//display form 2
} else if(isset($_POST[submit2])) {
//validate + save session data from form2
//display form 3
} else {
//display first form
//<input type="submit" name="submit1" value="Continue">
}