scriptygoddess

02 Sep, 2004

One form – two actions

Posted by: Jennifer In: Lessons learned

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

1 Response to "One form – two actions"

1 | » Retaining Form Data

February 19th, 2008 at 1:08 pm

Avatar

[…] So here's what i found I could do, with the help of a blog entry at scriptygoddess. […]

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements