0

My workaround getting the "Resubmit post data" dialog when users refresh, and respectively sending stuff twice, was to force a refresh on the page via javascript when content is posted. Which seemed to work in webkit browsers and IE but unfortunately firefox doesn't work that way.

The problem is that after the post I need the user to be returned to the same page which kinda confused me on using the post/redirect/get method since it is described there that another page needs to be supplied. Even if I send a redirect header from php itself firefox still asks about resubmitting. Can anyone suggest how I can solve this problem? Thank you in advance!

EDIT: Here's some code

if($_SERVER['REQUEST_METHOD']=="POST"){
    $user->validateSettingsData($_POST, TRUE);
    echo "<div class='win box10'>Changes saved, please wait..</div>";

    header("Refresh: 2; url=");
    exit();
}
php_nub_qq
  • 15,199
  • 21
  • 74
  • 144
  • Just add a nonce and ignore the input if the nonce is not valid – Anigel Aug 29 '13 at 15:43
  • I'm sorry I don't understand :? – php_nub_qq Aug 29 '13 at 15:43
  • Why not just use Ajax – Funk Forty Niner Aug 29 '13 at 15:46
  • @Fred-ii- because I can't upload images with ajax – php_nub_qq Aug 29 '13 at 15:47
  • 1
    http://stackoverflow.com/questions/638494/stop-data-inserting-into-a-database-twice – Anigel Aug 29 '13 at 15:47
  • @php_nub_qq Probably not with what you presently have. I can upload via Ajax no problem. Which wasn't in your question (uploading). – Funk Forty Niner Aug 29 '13 at 15:48
  • @Fred-ii- I aim not to use additional libraries and plugins. The only way I can think of uploading images without any of those is using the FILE API which is supported only in new browsers that average users may not necessarily have : – php_nub_qq Aug 29 '13 at 15:50
  • @php_nub_qq Not all Ajax scripts need additional libraries/plugins to upload. And I get what you say, try and find one that will do that. I would search, but I'm in the middle of something right now. But there are scripts out there. All you need to do is to pass your Ajax url to your uploading handler. – Funk Forty Niner Aug 29 '13 at 15:53
  • `header("Refresh: 2; url=");` is that it? or is that just an example? – Funk Forty Niner Aug 29 '13 at 15:56
  • That's what I use to refresh on the same page, since it's a dynamic url. `site.com//settings` – php_nub_qq Aug 29 '13 at 15:57
  • It's your workaround, you made a mistake with that, how can we help you then? And for the book: Post/Redirect/Get works with your version of Firefox 23.0.1. - Try for yourself (code-example): http://codepad.viper-7.com/6vLnSH/55dev? – hakre Aug 29 '13 at 15:59
  • 1
    and better use the correct header, it's `Location: ` not `Refresh: `. And yes, the URL is dynamic so provide the correct one. If you don't know how to do that, ask that instead of complaining redirects wouldn't be working. – hakre Aug 29 '13 at 16:01
  • @hakre try submitting a form on a certain page which submits to itself and then refresh from that very page and see what happens – php_nub_qq Aug 29 '13 at 16:01
  • Well, you still want to tell me you don't know the URI to use in the location header (same for the URL in the redirect header)? There is a good chance a single function call will do it all for you (on standard system configurations): http://php.net/function.http-redirect – hakre Aug 29 '13 at 16:03
  • I have edited my answer so that it refresh the same page even with dynamic URL by using `$_SERVER['REQUEST_URI']` – invisal Aug 29 '13 at 16:04
  • @hakre it doesn't matter if I supply the url or leave it blank it still sends a valid header the problem is not with refreshing but with how firefox suggests to resend the data.. – php_nub_qq Aug 29 '13 at 16:04
  • @php_nub_qq, can you try to use `header` the same way I did and see if it works? – invisal Aug 29 '13 at 16:06
  • @php_nub_qq: Okay, now you're running in circles. As I already wrote *and* demonstrated (the code example which works online is linked), there is no such issue in Firefox 23.0.1 if you use the redirect properly. I can not add more to this. – hakre Aug 29 '13 at 16:06

1 Answers1

1

You can use PHP to redirect. For example:

if (isset($_POST)) {
    // processing the data
    // ....

    header('LOCATION: ' . $_SERVER['REQUEST_URI']); // <-- for dynamic URL
    exit();
}
invisal
  • 11,075
  • 4
  • 33
  • 54
  • `Even if I send a redirect header from php itself firefox still asks about resubmitting.` – php_nub_qq Aug 29 '13 at 15:44
  • @php_nub_qq: Which firefox version? Firefox normally adheres the PRG pattern, see http://en.wikipedia.org/wiki/Post/Redirect/Get – hakre Aug 29 '13 at 15:46
  • @hakre my firefox is `23.0.1` I believe that's the latest version at this point – php_nub_qq Aug 29 '13 at 15:47
  • @php_nub_qq, can you show us some code where you use PHP redirect? – invisal Aug 29 '13 at 15:51
  • @php_nub_qq: I can confirm you that with that exact Firefox version PRG works. you are just doing an error elsewhere perhaps. – hakre Aug 29 '13 at 15:51
  • @invisal I added some code – php_nub_qq Aug 29 '13 at 15:55
  • This seems to work but I need to wait some time after performing the post because for some reason it takes a few seconds for changes to take effect.. sleep() doesn't seem to do anything O.o – php_nub_qq Aug 29 '13 at 16:15
  • @php_nub_qq, that is two entirely different question. POST/Redirect/GET = you send data using method 'POST', then redirect to the same page instantly to prevent resubmitting data. (no waiting time). Since you want to have 2 seconds waiting time to save your message, you can redirect to different page and redirect back to original page. – invisal Aug 29 '13 at 16:20
  • @invisal How haven't I thought of that??? THANK YOU! – php_nub_qq Aug 29 '13 at 16:35