1

Follow me on this one :-)

Pages: index.php contact.php

I go from index.php to contact.php, do a HTML POST to contact.php (where I send the data to a DB...), and then have a back button to go back to index.php. Now im looking for a Javascript or PHP solution to go back (exactly link javascript history.back(-1)) but I don't want the page to do the POST of contact.php again and I don't want to use history.back(-1). Now i know I can just say Back but I dont want to hard code it. And suggestions?

PHP Noob
  • 413
  • 1
  • 6
  • 19
  • 3
    Use AJAX to do your POST rather than loading another page – Andy Holmes Oct 03 '14 at 07:58
  • You could use `location.href =` or `location.replace()` or `window.location =` http://codequake.co.uk/code/javascript_jquery/6/Refresh_a_page_with_Javascript_or_jQuery – SubjectCurio Oct 03 '14 at 08:01
  • Did something solve your question? – Wouter0100 Oct 03 '14 at 08:08
  • Are you sure you wish to actually go back or do you just want to always go to the index.php page? Because that's a different question. – Alternatex Oct 03 '14 at 08:19
  • No the example about is not the exact scenario. I'm basically create like a DB folder system (like your folder system on your PC). So a user can go and create a folder and then open it, create more folders in that folder or place files there. But there are no folders created on the server. Like a virtual folder system and I work with parent and child id's. So I had the problem that when a user is in a folder system, and he creates a folder (POST) and wanted to go back, its wanted to rePOST the data. So I used the AJAX route and works perfectly. Also @Sharky your example works if you dont want – PHP Noob Oct 03 '14 at 08:41
  • to use Ajax. Hope this helps people in the future – PHP Noob Oct 03 '14 at 08:42

4 Answers4

4

You should use the POST-REDIRECT-GET pattern.

The page which accepts the POST data should respond with a 303 header and redirect to somewhere else.

// this is the file which accepts the post data
header("HTTP/1.1 303 See Other");
header("Location: http://$_SERVER[HTTP_HOST]/afterthepost.php");
die();

then in that page if user goes "back" (by any means-javascript, back button), he will not have the post submited again or be asked by the browser for resubmission.

a really nice example, easy to understand here : http://wordsideasandthings.blogspot.com/2013/04/post-redirect-get-pattern-in-php.html

Sharky
  • 6,154
  • 3
  • 39
  • 72
2

I have been looking for a reliable back button, here is the solution I like better than history.go(-1) or $_SERVER["HTTP_REFERER"] or others that use header and session tricks like these:
header('Cache-Control: no cache');, session_cache_limiter('private_no_expire'); which I found equally unreliable for my application.

Background: I have a search form that gets you to a result set page. Results page has typical CRUD-type functionality to view or edit a record using both GET and POST. Result set page also has links to other subpages to add/edit various record details. Users need to be able to navigate to the result page and the subpages and still be able to get back to the original result set.

Solution: The search form sends the user to an intermediate page which sets some session variables that retain the original search parameters and then directs the user to the page that displays the result set.

The search page is a basic search form which includes a search field and search value.

intermediate page:

session_start(); 

// Unset the variable if it exists 
unset($_SESSION['search']);
unset($_SESSION['field']);
unset($_SESSION['value']);   

if (isset($_POST['search'])){
    $_SESSION['search'] = $_POST['search'];
    $_SESSION['field'] = $_POST['field'];
    $_SESSION['value'] = $_POST['value'];   
}

// All sessions are set so redirect user to the results 
header('Location: https://www.yourwebsite.com/results.php');

back button code used:

<a href='results.php'> &lt; Back </a>

I'd be happy to hear if anyone can improve upon this solution. I'll try and set up a working example on fiddle or someplace if there is interest.

YakovL
  • 7,557
  • 12
  • 62
  • 102
Mike Volmar
  • 1,927
  • 1
  • 22
  • 31
0

You might use anything like history.go(-1) or history.back() but for user not beeing prompted to submit post data once again, you need to remove caching for the desired page (see this link)

Community
  • 1
  • 1
nicolas
  • 712
  • 4
  • 15
0

Use below PHP code for back button

<a href="<?php echo $_SERVER['HTTP_REFERER']; ?>" > Back </a>