Jump to content

Implementing the Post / Redirect / Get Pattern


bracketfire
 Share

Recommended Posts

Let's say the user submits a form.  If I then process the form and return a page, and the user reloads that resultant page, they are hit with the famous "Are you sure you want to resubmit the data" dialog.

In other apps I've written, I've avoided this by using the http://en.wikipedia.org/wiki/Post/Redirect/Get (Post Redirect Get / PRG) pattern.

Essentially, I handle the CRUD or other database work when processing the POSTed page, then save any necessary values in session and redirect to the same page via a 302 redirect GET.  That way the POSTed page doesn't appear in the browser history, and they can reload freely with no side effects, and if they hit BACK they go back to the form and aren't prompted to re-post the data.

I'm wondering if others do this and if anyone has any neat processwire-ish ways of handling it.

In particular, is it acceptable to save the posted data in session with

$session->PRG = $input->post;
$session->PRGPAGE = $page->id;
 

so that any values needed in the following screen (to generate a confirmation message, etc) are available from session.

In my system Template Files are controllers derived from a base class, so on each subsequent page, if the page doesn't match PRGPAGE, I'd clear the $session->PRG data so it doesn't hang around as the user moves around on the site.

Does this make sense?  Anyone else doing something like this to avoid POST resubmits?

  • Like 2
Link to comment
Share on other sites

Sure it makes sense, especially the redirect part. And there's nothing wrong with saving something to the session either. This is pretty simple stuff and I'm not sure there are too many ways to do it - so no ProcessWire tricks to share.

Looks like you've nailed it already, nice and simple. Only thing I haven't tried is saving the whole post data at once into session. And if that didn't work, you could always save individual values instead of the whole structure.

So nothing much to say, but didn't want to leave your question unanswered either :).

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...