I’m writing a web page where the visitor will click on a place on the screen, which will record where they clicked and redraw the page with new stuff.
Although the page doesn’t look like a form, I’m using the form construct so that I can pass the click location to the php script via a POST array, which will then save the information in the database.
What I’ve run into is what happens if they hit the refresh button/key combo/icon. If they do this, it resends the old data to the screen, even though I’m effectively finished with it.
Anyone here had to deal with this sort of issue? How did you do it?
I thought that most web browsers would not resend POST data without first asking the user.
And while I’ve not dealt with the problem, I do have an idea: would it break your page to automatically filter out clicks from the exact same location? Or would this break some needed functionality?
First a technical comment: POST forms are supposed to be used only for actions that have side effects (typically, store data or send email, that sort of thing) which you don’t want to re-execute when reloading a page (this is why browsers tend to ask you to confirm before reloading a POST request). If your form doesn’t actually do anything like that, you can just as well use a GET form.
If your action actually DOES have those side effects, the typical way of dealing with this is to have 2 requests:
Request 1 (POST) performs the action and stores the posted data somewhere (in a database record, or the session, for example) and returns a redirect response from where the data can be retrieved (if needed).
Request 2 is automatically performed by the browser following the redirect response (this is always a GET request) and retrieves the stored data and shows it (if needed) in the new page .
Any reloads at this point will repeat request 2. Using the back button will skip to the original form.
One solution to this general type of problem is called Redirect After Post, or Post-Redirect-Get. I think this is what SP is referring to. Google will turn up plenty of detailed descriptions.