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:
- user submits an add/edit form for a resource
- 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)
- 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:
- user submits an add/edit form for a resource
- 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:
- save users one click (they have to go through a lot of these add/edit forms)
- 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.
ASP.NET
Since the question is presented as language agnostic, you might be interested in ASP.NET’s
Server.Transferwhich 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:
This piece of code takes
something.phpon server side, allows to send POST data to it and shows you its content (which could be justprint_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.