Back button re-submit form data ($_POST)
If you are submitting a form with search parameters, you are trying to get some data, not modify some.
So, you should use the HTTP GET method, and not POST : POST should be used when you intend to create/modify data, and GET should be used when you intend to fetch some data.
Or, if you have some create/modify operation that has to be done :
- The form first POSTs to a first page
- That page does some operations (like writing something to a database)
- And then redirects to another page, using a
Location
HTTP header.
- It's that last page, that's queries by the browser using a GET requests, that displays the data fetched from the parameters received in the URL.
See the Post/Redirect/Get page on wikipedia, about this.
Prevent resubmit form after click back button
Rather than
echo '<script language="javascript">window.location="page2.php";</script>';
you should use the header() function to redirect your user after the submission.
So in psuedo code,
click submit on page.php action page1.php
page1.php submits data to database calls
header('Location: http://example.com/page2.php');
This should prevent your clicking back problem
How to Prevent Form Resubmission when page is refreshed or back button is clicked
I was searching around for days and finally found something. IF you use a HTML command it will remove any input the user put when the user goes back. Because my problem was when the user goes back after be redirected, their information was still there but if you use
<form method="post" enctype="multipart/form-data" autocomplete="off">
it removes everything so it kinda helps. The user will still be allowed to go back but at least now they can't resubmit the data.
How do you make the browser re-post form data when using the back button?
The only way to handle it is via a session... HTML5 allows for storing of that kind of data but to be honest I wouldnt even look into it as a possibility just yet, altough it does work.
Prevent form resubmit after pressing back button
You can use a javascript function with the help of a hidden attribute to reload the web page. When the user press the back button,based on the value of the hidden attribute, page will be reloaded without loading the cached page.
Your approach of clearing cache is correct. Coupled with that, you can use this approach.
<input type="hidden" id="refreshed" value="no">
<script type="text/javascript">
onload=function(){
var e=document.getElementById("refreshed");
if(e.value=="no")e.value="yes";
else{e.value="no";location.reload();}
}
</script>
One drawback of this approach is if your clients' browsers have disabled JS, this will not work.Otherwise it should work.
Related Topics
Get Youtube Video Id from HTML Code with PHP
The Advantage/Disadvantage Between Global Variables and Function Parameters in PHP
How to Separate Date and Time from Datetime in MySQL
Populate Another Select Dropdown from Database Based on Dropdown Selection
Unexpected Character in Input: '\' (Ascii=92) State=1
Casting an Array with Numeric Keys as an Object
PHP Recursion to Get All Possibilities of Strings
How to Add a Delete Button to a PHP Form That Will Delete a Row from a MySQL Table
Convert Ascii to Utf-8 Encoding
Sqlsrv_Num_Rows Not Returning Any Value
PHP Exec $Path Variable Missing Elements
Correct Way to Set Bearer Token with Curl
Passing PHP Variable into JavaScript
Can't Pass MySQLi Connection in Session in PHP
Returning a Value in Constructor Function of a Class