2

I have some code where I'm trying to prevent a form from being submitted multiple times. I found this code on another Stack thread but it doesn't work. Instead it submits the form infinite times and lags the entire server!

$(document).ready(function() {
    $('form').submit(function() {
        console.log("hi")
        if ($(this).valid()) {
            $(this).submit(function(){
                return false;
            });
            return true; //Tried with an without
        }
    });
});

Here is a picture of the output in the console:

enter image description here

This keep submitting the form. I just took a picture at that number.

The thread that I found the code above from is the accepted answer on this question and it has many upvotes.

Note that I have multiple forms per page, and many pages with many forms! A solution to one specific form is not sufficient. I need a global solution. Also I'm using Codeigniter.

Community
  • 1
  • 1
  • Well it will cause a infinite loop, (1), because your checking the event form's submit, (2), and inside your submitting the form, go back to (1). Try getting rid of the `submit()` callback, and attach the code to a simple button. – Keith Nov 18 '16 at 16:37

4 Answers4

0

You need to cancel the initial form submission so that you only do it after you've validated it. But frankly, HTML5 won't submit if a form is not valid, so your code seems redundant.

Here is a working example that takes most of the JQuery out of the mix. It only submits the form one time.

You can also test it here: https://jsfiddle.net/5oyp6aa0/6/

$(function() {
    var theForm = document.querySelector("form");
    theForm.addEventListener("submit",function(evt) {
        // First, cancel the form's submission:
        evt.preventDefault();
        evt.stopPropagation();
        console.log("Navitve Submit Cancelled")
        if (theForm.checkValidity()) {
            // Then, only if it's valid, submit
            console.log("Manual Submit Triggered");
            theForm.submit();
        }
    });

});
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
0

Try passing in the event and using e.preventDefault()

$(document).ready(function() {
    $('form').submit(function(e) {
        e.preventDefault();
        console.log("hi")
        if ($(this).valid()) {
            $(this).submit(function(){
                return false;
            });
        }
    });
});
0

Correct me if I'm wrong but, shouldn't you just return; if it's valid, else stop submission like in the jQuery documentation's example.

$(document).ready(function() {
    $('form').submit(function(event) {
        console.log("hi")
        if ($(this).valid()) {
            return;
        }
        event.preventDefault();
    });
});
Steven B.
  • 8,962
  • 3
  • 24
  • 45
0

After none of these answers worked I decided to give it another shot. This works for me.

$(document).ready(function() {
    $("form").submit(function (e) {
        var attr = $(this).attr('submitted');
        if (typeof attr === typeof undefined || attr === false) {
            if ($(this).valid()) {
                $(this).attr('submitted', 'submitted');
                $(this).submit();
            } else {
                e.preventDefault();
            }
        } else {
            e.preventDefault();
        }
    });
});
  • 1
    You can get rid if both else branches. You dont need downvoting just everything. You may mark your answer as accepted if thats the fact. – alariva Nov 18 '16 at 18:30