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

stop form resubmission on page refresh javascript
prevent form from refreshing the page
how to avoid resending data on refresh in php
prevent form resubmission on refresh jsp
stop inserting data into database when refreshing the page in php
prevent form resubmission on refresh c#
php disable refresh
after form submit refresh page

I have a simple form that submits text to my SQL table. The problem is that after the user submits the text, they can refresh the page and the data gets submitted again without filling the form again. I could redirect the user to another page after the text is submitted, but I want users to stay on the same page.

I remember reading something about giving each user a unique session id and comparing it with another value which solved the problem I am having but I forgot where it is.

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 stop form resubmission on page refresh? Webtrickshome, Instead of … } else { echo "<p>Your file has been uploaded.</p>"; } … redirect to another address on success: } else { $new_url = add_query_arg( 'success', 1,  Questions: I have a simple form that submits text to my SQL table. The problem is that after the user submits the text, they can refresh the page and the data gets submitted again without filling the form again. I could redirect the user to another page after the text is submitted, but I want

I would also like to point out that you can use a javascript approach, window.history.replaceState to prevent a resubmit on refresh and back button.

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

Proof of concept here: https://dtbaker.net/files/prevent-post-resubmit.php

I would still recommend a Post/Redirect/Get approach, but this is a novel JS solution.

How to stop form resubmission on page refresh, How to prevent form resubmission when page is refreshed (F5 / CTRL+R) - php. How to stop form resubmission on page refresh? Basically, 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 multiple ways.

You should really use a Post Redirect Get pattern for handling this but if you've somehow ended up in a position where PRG isn't viable (e.g. the form itself is in an include, preventing redirects) you can hash some of the request parameters to make a string based on the content and then check that you haven't sent it already.

//create digest of the form submission:

    $messageIdent = md5($_POST['name'] . $_POST['email'] . $_POST['phone'] . $_POST['comment']);

//and check it against the stored value:

    $sessionMessageIdent = isset($_SESSION['messageIdent'])?$_SESSION['messageIdent']:'';

    if($messageIdent!=$sessionMessageIdent){//if its different:          
        //save the session var:
            $_SESSION['messageIdent'] = $messageIdent;
        //and...
            do_your_thang();
    } else {
        //you've sent this already!
    }

How to prevent a form from resubmitting when the page is refreshed , This method is quite easy and blocks the pop up asking for form resubmission on How can we refresh a DIV without reloading the whole page? Learn how to set up and deploy F5 DNS cloud service. but solves the problem inherently, would be control your form and submission with javascript (using ajax requests). Stop HTML Form Resubmit on Refresh Page in Javascript. How to prevent PHP HTML form resubmission when page is refreshed/reload (F5/CTRL+R)? One way to prevent resubmit data is to use the javascript history.replaceState method.

I use this javascript line to block the pop up asking for form resubmission on refresh once the form is submitted.

if ( window.history.replaceState ) {
  window.history.replaceState( null, null, window.location.href );
}

Just place this line at the footer of your file and see the magic

How to prevent resubmit on refresh or reload page in php?, How to prevent form resubmission when page is refreshed (F5 / CTRL+R). I have a simple form that submits text to my SQL table. The problem  Here Mudassar Ahmed Khan has explained with an example, how to prevent / avoid / stop Form Resubmission (Resubmit) when Refresh button clicked in Browser in ASP.Net. The problem is that when browser refresh button or F5 or CTRL + F5 function keys are pressed, the last executed event is again executed. For example, if you have a form submission on Button click and after clicking button, page is

You can prevent form resubmission via a session variable.

First you have to set rand() in a textbox and $_SESSION['rand'] on the form page:

<form action="" method="post">
  <?php
   $rand=rand();
   $_SESSION['rand']=$rand;
  ?>
 <input type="hidden" value="<?php echo $rand; ?>" name="randcheck" />
   Your Form's Other Field 
 <input type="submit" name="submitbtn" value="submit" />
</form>

After that check $_SESSION['rand'] with textbox $_POST['randcheck'] value like this:

if(isset($_POST['submitbtn']) && $_POST['randcheck']==$_SESSION['rand'])
{
    // Your code here
}

Form resubmission when page is refreshed (F5), How to prevent form resubmission when page is refreshed (F5 / CTRL+R). Viewing 1 replies (of 1 total). Plugin Support sharmadpk03. (@sharmadpk03). I submit the form, and all is well, however if I try to reload the new page that the form goes to after submission, I get the "Do you want to resend data" message (FireFox). It might happen in other browsers too, but I'm not sure. Does anyone know of a way to stop this message popping up so I can go ahead and refresh the page.

Prevent (Avoid) Form Resubmission (Resubmit) when Refresh , How to prevent form resubmission when page is refreshed (F5 / CTRL+R) Questions: I have a simple form that submits text to my SQL table. The problem is that  To me, in most cases even having the "Confirm form resubmission" dialog pop up shows that there is a design flaw. By the very nature of POST being used to perform destructive actions, web designers should prevent users from ever performing them more than once by accidentally (or intentionally) refreshing the page.

How to prevent form resubmission when page is refreshed (F5 / CTRL R). Question. I have a simple form that submits text to my SQL table. The problem is that  Final Words. So, these were some of the ways to fix the Confirm Form Resubmission dialog’s pop up on Google Chrome browser. These might not be the permanent solution of the problem and you might need to do this periodically.

The problem is that when browser refresh button or F5 or CTRL + F5 function clicking button, page is refreshed by the user then the form is resubmitted and  Unset Form Data. 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.

Comments
  • Post/Redirect/Get.
  • Why do you not want to redirect user to another page?
  • @Adam: Because this is excess to do another request to the server which in turn will fetch some data from DB again. But this is resource wasting because we already fetch all required data while processing POST request
  • @EugenKonkov in the PRG pattern, you would just redirect to a page that shows a success message. No further fetching from DB needed.
  • @Adam: You also may display the whole record which is created by POSTing data. In this case you need to SELECT it from DB. For example when you create the invoice you are redirected to /invoices/53 which display the whole invoice instead of just 'success'
  • This makes chrome unusable for development where business is requiring only a post!
  • If you use the PRG pattern, then you actually leave the page right? Wasn't the question how to make things work when not redirecting?
  • It seems to me that if you redirect to the same page, then $_POST is cleared. As I understand it, that was the desired effect. That was MY desired effect, anyway. I think the answer would be better if it made that explicit, though.
  • Works well, my only recommendation is to make sure you enable strict error testing so you're catching errors locally while developing, otherwise they can go unnoticed.
  • Doesn't answer the question. Not sure why it's the accepted answer.
  • Thanks, @dtbaker, it's awesome :)