0

I currently have a PHP page called item.php that has a link on it "Add Item".

When a user clicks the link, it takes them to save.php, where the page does some stuff like saving the item to the database. On that page, there is a redirection after the saving code has run:

header("Location: item.php);

Now the item.php page says "Remove Item" for the link, as it should.

Here's the problem: if the user now hits the Back button on their browser, it shows them the item.php page again (which I don't want) and shows them the "Add Item" link again (which I also don't want).

How do I solve this issue so that there is only one item.php page in the browser history stack?

Ethan Allen
  • 14,425
  • 24
  • 101
  • 194
  • 1
    Have you considered using jQuery and AJAX to add the items while not refreshing the entire page? It seems like that would be the most elegant solution to your problem. – Charles Nov 28 '13 at 18:41
  • That does sound like the best solution, I am just unfamiliar with both (I'm normally a desktop/mobile app dev) so I was seeing if there was something simple I was missing here. – Ethan Allen Nov 28 '13 at 18:44
  • Just to clarify, what happens when the user refreshes `item.php` after hitting the back button? It should say "Remove Item" after the refresh, shouldn't it? – Charles Nov 28 '13 at 18:47
  • Yes, upon a refresh it does appear correctly. – Ethan Allen Nov 28 '13 at 18:48
  • First of all you have to take care of cache control: http://stackoverflow.com/questions/20088616/browser-back-button-showing-the-page-from-cache The second is you have to make on item.php validation the page which the request comes and redirect it if necessary. – Ruben Kazumov Nov 28 '13 at 18:59
  • Check out my answer and see if it helps you out :) – Charles Nov 28 '13 at 19:00
  • @RubenKazumov Can you give me more details on the "second" thing you describe? – Ethan Allen Nov 28 '13 at 20:18

1 Answers1

1

In you're not willing to use AJAX, I think the real question you mean to ask is "How can I refresh item.php when the back button is pressed?"

There are several ways to achieve this, here's one that's done using JavaScript/jQuery. Store a hidden <input> variable called "refresh" and submit it when the user clicks "Add Item":

<input type="hidden" id="refresh" value="no">

Now, you can use the following jQuery function:

$(document).ready(function(e) {
 if ($("#refresh").val() == 'yes') { location.reload; } else { $('#refresh').val('yes'); }
});

Here's how this code works: The first time you load the page and call $(document).ready, the hidden input value is "no", which means the aforementioned function will set the value to "yes". Then, when the user hits the "back" button, the hidden field value will remain the same (it will still be yes), but $(document).ready would be called again. This time when it is called, it will reload the page.

Related reading: http://www.hunlock.com/blogs/Mastering_The_Back_Button_With_Javascript

A solution using PHP: How do I make Firefox reload page when back button is pressed?

Let me know if my explanation wasn't clear or if you have any questions. Hope this helps.

Community
  • 1
  • 1
Charles
  • 4,372
  • 9
  • 41
  • 80