8

I want to create a button to reload a page without losing $_POST data and $_SESSION.
On the web, I found this piece of code:

onclick="document.location.reload();"

And here is the code of my button:

<a class="button" href="" style="font-size: 0.7em; padding: 5px 10px;" onclick="document.location.reload();">Recharger la page</a>

But when I click on the button, I lose $_POST data and $_SESSION.

If I try with the keyboard command Ctrl+R (Chrome) or F5 (Firefox, IE9), the browser is showing an alert to notify me that I'm again trying to submit form. If I accept, it works.

How can I reproduce this kind of browser-refresh with a JavaScript command? Or is the code of my button wrong?

Thank you very much for your help.

user2428118
  • 7,935
  • 4
  • 45
  • 72
Zorkzyd
  • 929
  • 3
  • 12
  • 30

3 Answers3

9

Try using

location.reload(true);

This will perform a "hard" refresh, not just rebuilding the DOM but also re-retrieving any resource from the server.

You can read more at the Mozilla Developer wiki.

Apparently, location.reload() is the equivalent of F5 in scripting, whilst Ctrl+F5 / Ctrl+R can be simulated using location.reload(true).

Also, as ThiefMaster mentioned, you're missing ;return false at the end of your onclick statement, or you should set the href to javascript:void 0* to prevent the browser from following the link.

*Or any other piece of JavaScript that returns undefined

Community
  • 1
  • 1
user2428118
  • 7,935
  • 4
  • 45
  • 72
  • When I use location.reload(true), browser is giving the alert to notify that I'm again trying to submit form. If I accept, it doesn't work. Is it due to the href=""? – Zorkzyd Jun 02 '12 at 09:38
  • That's because the browser will reload using exactly the same parameters and request method as it used when it loaded the page for the first time. If you don't want to repeat this, you could better first store the data in $_SESSION and then do a `header("303 See Other")` and a `header("Location: $_SERVER['REQUEST_URI']")` to reload the page with a GET request. – user2428118 Jun 02 '12 at 09:50
  • Thank you for your answer but ThiefMaster gave me the solution :) – Zorkzyd Jun 02 '12 at 09:51
  • 2
    `location.reload(true)` doesn't POST anything, GET request is used instead. – Maksim Vi. May 14 '14 at 19:52
2

This should happen in any case as long as you are on the same location you POSTed to. However, it's common to redirect after a POST request to avoid exactly what you are trying to do.

The reason why your code doesn't work is the fact that href="" will cause a GET request to the current URL. Use href="#" to prevent it from loading a "new" page or add return false; at the end of your onclick="..." code.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • When in press F5, in Firefox for instance, It works fine. I just want to find a way to do that with a javascript button ? – Zorkzyd Jun 02 '12 at 09:42
  • `location.reload()` should do the job as you can see on http://jsfiddle.net/ThiefMaster/thTyD/ (note that you must click "Run" first so the output pane was actually loaded via POST). – ThiefMaster Jun 02 '12 at 09:43
  • But my code above doesn't work… onclick="document.location.reload();". Is it due to the href=""? – Zorkzyd Jun 02 '12 at 09:45
  • 1
    Yes, you need to use `href="#"` or `return false;` at the end of your onclick. – ThiefMaster Jun 02 '12 at 09:46
  • `href="#"` always jumps to the top of the page. [Try this link for instance.](https://stackoverflow.com/questions/10861176/location-reload-loses-post-session-data-f5-ctrlr-keeps-data/#). That's really annoying. For this reason, I recommend using `javascript:void 0` (or any JavaScript that returns `undefined`) instead. – user2428118 Apr 24 '14 at 11:44
0

The Ctrl + R refreshes the page and clears your cache. And I guess you're using Internet Explorer? Some other browsers behave like this when you hit Ctrl + F5, but not with Ctrl + R

Sources:
https://superuser.com/questions/205279/ctrlf5-vs-ctrlr-on-browsers
Browser issue in Ctrl-R

Community
  • 1
  • 1
Tschareck
  • 4,071
  • 9
  • 47
  • 74