It's a good thing I have no shame in admitting when I was doing something stupid. 😉 Otherwise I'd have no posts for this category.
Just to explain a little about what I was working with – it was essentially an email form (emailform.php) – but before sending the email, it brought you to a page where you could preview your email. From the preview page (preview.php) you needed to have the option to go back and edit your message (back to the first form: emailform.php), or to send the email (sendemail.php). (Obviously when you go back to emailform.php – I needed to have it "remember" what you originally wrote there)
I won't even tell you how I was handling this before – because it was stupid, I know it stupid, but it worked, almost. But I knew there was a better way. Today with some time on my hands I found this page with EXACTLY what I needed.
So now the form tag on my preview.php just calls itself and looks something like this:
<form action="preview.php" method="post">
And my submit buttons simply look like:
<input type="submit" name="Goback" value="Go back and edit">
<input type="submit" name="Sendemail" value="Send Email">
Then, at the very beginning of preview.php (before the first HTML tag) I have:
if (isset($_POST["Goback"])) {
header("Location: emailform.php");
} else if (isset($_POST["Sendemail"])) {
header("Location: sendemail.php");
}
/*
I'm setting the session variables AFTER the above because otherwise those "submit" buttons become persistent when preview.php submits the current page to itself. This way – only the data being sent to this page from the ORIGINAL form (emailform.php) become persistent in session cookies.
*/
foreach($_POST as $k=>$v) {
$_SESSION[$k]=$v;
}
On the both the emailform.php page, and the sendemail.php page, wherever I looked for values in $_POST – I change to now look for the same in $_SESSION.
This way – if you click the back button from the preview page, the data is not forgotten.
(I know this probably won't make a whole lot of sense to many people – and those it does make sense will just wonder why/how I just figured this out NOW.)