I've looked around for an answer to this problem, and I'm slowly accepting the fact that I've probably gone about things the wrong way, but here's a request for some wonderful pearl of wisdom that might help!

I'm working on a website which has a directory-style navigation bar on the left. So at first the user will only see two directories (Grammar / vocabulary - it's an English learning site) and by clicking on the directory, it will "open" to reveal its contents (other pages or directories).

The way I have this working is that each directory is a form submit button that reloads the page when clicked, but with a value in $_POST that tells the site to open or close a folder (this is done within an array in $_SESSION). It works great! The issue, though, is that if a user clicks the back button in their browser, they get the prompt asking if they wish to resubmit data, which is not something I want.

I was wondering if there was a way I can detect in php if the back button has been pressed, so that instead of reloading the page and making changes to the side bar, the side bar will remain the same and instead the user gets taken to the last page they had visited.

I'm aware that the real solution to this is probably just to rethink the side bar, and any advice on a better way to do that would be much appreciated also!

Michael

有帮助吗?

解决方案

The reason you're getting the prompt is because POST is supposed to be used if the data being sent is going to modify something, so submitting a POST request using the back button might have unexpected behaviour (like duplicate data).

Replacing POST with GET should disable the prompt.

HTML
From:

<form action="page.php" method="post">
</form>

To:

<form action="page.php" method="get">
</form>

PHP
Change references from $_POST[''] to $_GET['']

其他提示

<?php
        session_start();        
        if( $_GET['t'] == $_SESSION['token'] ||  $_SESSION['directory'] == $_GET['q'] || !isset($_GET['q']) ) { 
          echo "<h1><span>you going</span> forward</h1>";
        } else {
          echo "<h1><span>you going</span> backward</h1>";
          echo "<p>should be <strong>{$_SESSION['directory']}</strong> directory</p>";
        }        
           $token = uniqid();  
           $_SESSION['token'] = $token;
           $_SESSION['directory'] = $_GET['q'];  
?>

<a href="index.php?q=grammar&t=<? echo $token; ?>">grammar</a>
<a href="index.php?q=vocabulary&t=<? echo $token; ?>">vocabulary</a>

As a general rule in web development you should only do a post when you are submitting data that you don't want to be resubmitted, or that you don't want to show up in your logs on the server. And once you do a post, you should redirect using get parameters or some other method.

If you use get parameters instead of post then you can just make the page navigation stateless and just fetch the page they ask for.

There are methods for detecting if the back button has been pressed, but they are difficult to make work consistently, and can be frustrating to the user, so it is recommended you go about it another way.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top