开发者

How to deny HTML form submission if previous submission is still in action?

So I have this form which executes a开发者_JAVA技巧 pretty long data posting using AJAX, now I am a bit paranoid that some smart bloke might open firebug and re-trigger posting process, while previous is still in action. How do I avoid this?

Thanks!


You can:

  • Disable submit buttons.
  • Prevent Enter key presses.
  • Remove action attribute from form tag.
  • Redirect to another page upon first submission and expire the page with form on it.

With that said, there's only so much you can do on the front-end to prevent resubmissions - there's always a way around. If you are so concerned about this, then server-side validation is also necessary (and usually recommended anyway).


Set a condition when submitting the form, and add a check on form submission. Example in jQuery:

$('form').submit(function(e) { 

    // If form has been submitted
    if($(this).data('submitted') == true) {
        e.preventDefault();
        return;
    }

    // Set the condition
    $(this).data('submitted',true);

    //...

    //After form has been submitted successfully, release the condition
    $(this).data('submitted',false);
});

This is preferred to disabling parts of the form, since submission can occur from any input in the form.


The first thing is that given you're worried about multiple submissions as a result of circumventing client controls (i.e. using Firebug or Fiddler to reissue requests), you can forget about any option which applies controls at the client side (i.e. disabling buttons, JS checks on submission status, etc). This is one you'll have to handle on the server.

Have a look at the synchroniser token pattern. This is commonly used to help prevent CSRF (as is the context in the link), but it's also used to avoid multiple form submissions. If you can pass a token through in your AJAX request which is verified then invalidated after the request begins processing (obviously before the heavy work begins), you've got yourself a means of preventing multiple submissions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜