问题
Using the post/redirect/get pattern, you can prevent duplicate form submission. For example, you can prevent a visitor of your online shopping website from ordering the same item twice.
However, is it possible to block such a duplicate action but receive and maintain input values, which are send with POST, on DOM after the visitor refreshes the page without using AJAX call?
To clarify the point, here is an example.
- You have only one page, which has a
<form>, several<input>and submit button. - You need to use POST method, so that a specific function
(e.g. ordering an item)will be run after the button was clicked. You need to prevent this function from getting executed when refreshing. - You'll go back to the same page with an input value, having the value somewhere in the DOM.
- When you refresh the button, the function described at "2" must not be run. However, you must hold the input value even after the refreshing.
So, in other words...
You use POST when you clicked the button. The <input> values will stay in the DOM.
You use GET when you first arrive at the page. Of course there is no <input> value. However, when you refresh the page, you'll maintain the <input>.
This question can be subjective, but I'd really appreciate if you would give any insight, because I have nobody to ask for such a help.
(I use Laravel/PHP)
回答1:
You need to transport those values via GET.
There are two options to do that:
- sessions
- URL query parameters
If you simply save the data in the session, you can read it from there and re-populate the form. If you put it into the URL, you can read it from there; but obviously the URL will contain a lot of data then.
Reading it from a session the data will not be unique to the specific page/redirect, but anyway you open that page again within the same session will show the same data. Passing the data via the URL will make it unique to the specific request.
As a middle ground, you can save the data in the session tied to a specific random id, redirect with this id in the URL (e.g. example.com/foo.php?i=12345), the re-populate the data from the session with the specific id.
回答2:
I does not tested the below code, but I believe it pretty helpful to you.
It only works, if you are working on Laravel framework.
Please try this..
return redirect('/Your-page-url')
->withInput();
回答3:
It seems you're looking for session storage
来源:https://stackoverflow.com/questions/36692521/how-to-keep-post-methods-input-values-with-post-redirect-get