Home / প্রযুক্তি / ওয়েব সাইট / ওয়ার্ডপ্রেস / How to stop form resubmission on page refresh in Php?

How to stop form resubmission on page refresh in Php?

A problem happens when you do not redirect page but want to keep the page on the same page after form data submission. If you do this a general problem happens. If you refresh the page it will warn you to resubmit data. The general idea is to redirect the user to some other pages after the form submission which would stop the form resubmission on page refresh but if you need to hold the user in the same page after the form is submitted, you can do it in simple two ways.

Method-1 with PHP Code

One way to stop page resubmission on page refresh is to unset the form data after it is submitted so that the variable storing form data becomes empty and wrap up your form processing block of codes to check if the form is empty. Use this code at the beginning of the form posting script.

if(!empty($_POST) && $_SERVER['REQUEST_METHOD'] == 'POST'){
 $data = // processing codes here
 unset $data;
}

Method-2 with JavaScript

This method is quite easy and blocks the pop up asking for from resubmission on refresh once the form is submitted. Just place this line of javascript code at the footer of your file and see the magic. Use this code below the page.

<script>
if ( window.history.replaceState ) {
  window.history.replaceState( null, null, window.location.href );
}
</script>

About admin

Leave a Reply

Your email address will not be published. Required fields are marked *

*