Reloading the page after form submission  in POST method, browser will always display a warning  whether it can resend same data to server or not. For average user, this information is a bit technical and it might confuse him.

Some browsers even doesn't not ask this confirmation at all, it just resend the input data again and again. At that time if input values validation isn't proper in the backend then it create duplicate records.

For example, When we see warnings like "Do not click back button or refresh the page" in an online payment system, we may feel  bit nurveous and uneasy. Following scenarios makes post data resubmitted again and again:
 
  • Reloading the page again and again after form submission

  • Clicking back button then forward button in browser

  • Returning back to form in case of any redirection after form submission and then clicking submit button to resend the data

How to avoid form submission on page reload:

To avoid this issue we need to follow POST – REDIRECT – GET method along with avoiding browser cache. That means,
 
  • First we need to tell the browser do not cache this page
  • After successfully processing POST data, we need to redirect the user to same page in the backend. So browser consider this as fresh request and we can add another set of values.

Below I've given html - php example which implements above method. Save this code in htdocs directory as post.php

<html>
<!-- disallow browser cache -->
<meta HTTP-EQUIV="Pragma" content="no-cache">
<meta HTTP-EQUIV="Expires" content="-1" >
<?php
 if(isset($_POST['sdf'])){
  echo 'POSTED';
  header('Location:http://localhost/post.php');
 }else{
  echo 'no values' ;
 }
?>

<form action='' method='post'>
<input type='text' name='sdf' value='sdf'>
<input type='submit' value='submit'>
</form>
</html>

 


Comments (1)
  1. Image
    Mpatoh - Reply

    May 26, 2016

    Thanks! this is what I was looking for!!

Leave a Comment

loader Posting your comment...