tags:

views:

72

answers:

1

Looking at the previous implementation of a site that I'm working on, I can see that we have an options form that is calling a ManageObject.php script. That script does some actions on a number of objects, and then returns to the page via the following code:

echo "<script type=\"text/javascript\"> window.history.back();</script>";

My javascript skills are very beginner-ish, but what I can tell is that it's just pressing the back button on the browser. This of course does not refresh the page, so all the data on that page is now old.

Is there a way to force a refresh on calling window.history.back()? Or is there a better way of calling the .php script without leaving the page and refreshing the page after the .php script has completed?

+1  A: 

There is a system called POST-redirect-GET, which solves this problem. It works like this:

  • A form is submitted with POST data to a php page
  • The php script processes the POST data, and then redirects the browser to another page
  • Since the browser was redirected using GET, a refresh of the page will not resend the POST data. Also, the data on the page will be updated.

In PHP you can redirect by calling header("Location: /path/to/page.php"); followed by exit();. The advantage over the JavaScript way you posted in the question is that it does not require that the user has JavaScript enabled, and does not take the user to an intermediate page.

Marius
Are there any good tutorials that show this in action with a Form?
Sakamoto Kazuma
Nvmd Found a good one: http://ranacse05.wordpress.com/2008/04/28/header-tutorial/
Sakamoto Kazuma