Prevent comment form re-submission

Asked
Active3 hr before
Viewed126 times

6 Answers

prevent
90%

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.,This method is quite easy and blocks the pop up asking for form 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.,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 can join webtrickshome community and instantly start blogging, ask questions or answer other questions. All you need to do is register with your name and email address and click on the verification link that you will receive in email after submission of the registration form.

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.

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

I use this javascript line to block the pop up asking for form resubmission on refresh once the form is submitted. ,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.,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. Still, if you need to hold the user on the same page after the form is submitted, you can do it in multiple ways, but here I am describing the JavaScript method.,Redirection protects form from being resubmitted on page refresh.

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>
load more v
72%

WordPress Development Meta ,This will make $data empty after processing and the first clause would stop form resubmission on page refresh., WordPress Development help chat ,WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.

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, get_permalink());
   wp_redirect($new_url, 303);
   exit;
}
load more v
65%

So, every time when the form is submitted, a new act is generated, stored in session and compared with the post act.,It redirects to the same page, only when form is valid and action was performed.,if you are doing something like comments and you want the user to stay on the same page, you can use Ajax to handle the form submission,When the form is processed, you redirect to another page:

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>
load more v
75%

A lot of people go into computing just because they are uncomfortable with other people. So it is no mean task to put together five different kinds of Asperger’s syndrome and get them to cooperate.,If we want to do anything with the data the user sends, like store a record in the database, send an e-mail, deface NASA web page, whatever, we must do it before the redirect, in the logic for page A.,Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.,This weekend I was curious about how WordPress (3.3.1) solves this because I’m developing some projects with this CMS and I was surprised for my findings. The function wp_new_comment(), called from wp-comments-post.php calls wp_allow_comment(), both functions are located in comment.php, look what this last function does:

Now the solutions! First I will show you the way to prevent resending the form if the user navigates backwards or refreshes the page. The nicest solution is to implement two pages, let’s call them A and B. In page A the user submits the form and a Post request is send to the server. Our code must process this request and, if valid, resend it to the page B in a Get request. Let’s see server’s side code for page A for one of the nicest PHP frameworks out there, CakePHP:

public
function admin_proxy() {
   $params = array();
   foreach($this - > data['User'] as $key => $value) {
      if (!empty($value)) {
         $params[$key] = rawurlencode($value);
      }
   }
   $destination = array_merge(array('controller' => 'users', 'action' => 'admin_index'), $params);
   $this - > redirect($destination);
}
load more v
40%

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

Other "prevent-undefined" queries related to "Prevent comment form re-submission"