1

Hello,
I'm looking for advise on how to share certain bits of data (i.e. post-submit confirmation messages) between individual requests in a web application. Let me explain:

Current approach:

  1. user submits an add/edit form for a resource
  2. if there were no errors, user is shown a confirmation with links to:
    • submit a new resource (for "add" form)
    • view the submitted/edited resource
    • view all resources (one step above in hierarchy)
  3. user then has to click on one of the three links to proceed (i.e. to the page "above")

Progmatically, the form and its confirmation page are one set of classes. The page above that is another. They can technically share code, but at the moment they are both independent during processing of individual requests.

We would like to amend the above as follows:

  1. user submits an add/edit form for a resource
  2. if there were no errors, the user is redirected to the page with all resources (one step above in hierarchy) with one or more confirmation messages displayed at the top of the page (i.e. success message, to whom was the request assigned, etc)

This will:

  1. save users one click (they have to go through a lot of these add/edit forms)
  2. the post-submit redirect will address common problems with browser refresh / back-buttons

What approach would you recommend for sharing data needed for the confirmation messages between the two requests, please?

I'm not sure if it helps, it's a PHP application backed by a RESTful API, but I think that this is a language-agnostic question.

A few simple solutions that come to mind are to share the data via cookies or in the session, this however breaks statelessness and would pose a significant problem for users who work in several tabs (the data could clash together). Passing the data as GET parameters is not suitable as we are talking about several messages which are dynamic (e.g. changing actors, dates).

Thanks,
M.

MicE
  • 5,038
  • 2
  • 29
  • 26
  • 1
    I'm fairly sure this has been answered before in a different form - check this SO question out and see if it helps you - http://stackoverflow.com/questions/1058497/how-to-display-messages-to-the-user-after-a-post-http-redirect. (Alternately, try searching on the [Post-Get-Redirect] tag, since there are quite a few questions concerning this problem.) – Jeff Sternal Mar 24 '10 at 14:02
  • @Jeff: you are right of course. I didn't know that the proper term for the problem was Post-Get-Redirect (PRG), good to know. There is a lot of good alternatives how to approach it - thank you for the guidance, much appreciated! – MicE Mar 27 '10 at 23:05

2 Answers2

2

It sounds like type of user messaging system is needed. By this what I mean the process could look similar to the following:

  1. The user submits the form
  2. The system could register the user for a set of notifications/messages (store in a DB table or something along those lines.
  3. The system send the redirect response to the user
  4. On the next page load for the user the system would check to see if the user had any pending messages.
  5. If they do then remove them from the pending list/table and add them to the page

This keeps you from having a page state issue while still providing the user with the messages to be displayed, note that you might need to tweak the specifics to your needs.

Hope this helps.

Ken Henderson
  • 2,828
  • 1
  • 18
  • 16
  • Hi confusedGeek, thank you for the suggestion. This is actually an option I was considering myself, but using DB for storage seemed like an overkill in order to just address confirmation message handling. From a processing point, using $_SESSION would be more effective as in this case it's only a temporary storage - both alternatives however have to handle extra checks to prevent mixing of messages between individual page loads (i.e. users switching context / working in multiple tabs). – MicE Mar 27 '10 at 22:56
1

ASP.NET

Since the question is presented as language agnostic, you might be interested in ASP.NET's Server.Transfer which does exactly what you want to achieve.


PHP

However for PHP, the situation doesn't seem to be very easy and all solutions that come into my mind have design smells:

Sessions

Using sessions to mark your data with certain flags and then check and use them on overview page. You just have to be sure you always unset these data after you don't need them anymore, which might be tricky.

Database

Queue those data in database as confusedGeek described in his post, but I don't think it's a good idea to query every single request like this. It's going to be quite many requests on DB server if you application is bit bigger, which might slow things down.

cURL

Taking advantage of cURL in PHP, if you have the chance:

<?php
$curl = curl_init( );

curl_setopt( $curl, CURLOPT_URL, "http://localhost/something.php" );
curl_setopt( $curl, CURLOPT_POST, true );
curl_setopt( $curl, CURLOPT_POSTFIELDS, $_POST );

curl_exec( $curl );
curl_close( $curl );
?>

<form action="test.php" method="post">
    <input id="textfield" name="textfield" />
    <input type="submit" />
</form>

This piece of code takes something.php on server side, allows to send POST data to it and shows you its content (which could be just print_r( $POST ); in this example). This one could do what you need, but it has once again one flaw - the URL won't change, so users might get confused - and I wouldn't really recommend it.


I personally think your case might be a design flaw. Why would you want to take all resources from form page and move them to other? Isn't easier to work with your data in a file / class designed for it and then just decide what outcome you have? If something fails, it returns the user on page with form and if everything went well, you post the data to DB and show overview page with some happy message that everything went okay.

If you really want to proceed with the way of sending everything to other page, using AJAX/AJAJ could be another solution for you.

Community
  • 1
  • 1
Ondrej Slinták
  • 31,386
  • 20
  • 94
  • 126
  • Hi Ondrej, thanks for the feedback. Just to clarify a possible misunderstanding: I don't want to process the data on a different page - I want them to be processed by the same page/class which allows them to edit the data. The redirect would come into play afterwards, in order to address the two numeric bullets at the end of my question. – MicE Mar 27 '10 at 22:52