How to Stop My PHP Form Resubmitting Input After Page Refresh

How to prevent form resubmission when page is refreshed (F5 / CTRL+R)

Use the Post/Redirect/Get pattern. http://en.wikipedia.org/wiki/Post/Redirect/Get

With my website, I will store a message in a cookie or session, redirect after the post, read the cookie/session, and then clear the value of that session or cookie variable.

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.

After Submitting HTML form how to stop form resubmision when page refresh

<?php
if(isset($_POST['submit']))
{
//Your code comes here

//Redirect at the end of process
header('Location: http://www.example.com/redirect.php');
}
?>

Best way to avoid the submit due to a refresh of the page

Don't show the response after your create action; redirect to another page after the action completes instead. If someone refreshes, they're refreshing the GET requested page you redirected to.

// submit
// set success flash message (you are using a framework, right?)
header('Location: /path/to/record');
exit;

Preventing form resubmission

There are 2 approaches people used to take here:

Method 1: Use AJAX + Redirect

This way you post your form in the background using JQuery or something similar to Page2, while the user still sees page1 displayed. Upon successful posting, you redirect the browser to Page2.

Method 2: Post + Redirect to self

This is a common technique on forums. Form on Page1 posts the data to Page2, Page2 processes the data and does what needs to be done, and then it does a HTTP redirect on itself. This way the last "action" the browser remembers is a simple GET on page2, so the form is not being resubmitted upon F5.

Prevent browser form-resubmission alert

Do a redirect from post.php. Save data in session or in database and retrieve from redirect page.

Example Scenario:

  • Submit the form
  • Save the user record to db, get the id of the new record e.g. in $id
  • redirect using header, something like:

    header('Location: result.php?user_id='.$id);
  • get the user record from db, with the provided id and show it to the
    user.


Related Topics



Leave a reply



Submit