2

What is the Magento way to avoid multiple form submissions? Ideally I'd like to disable the submit button on the form "submit" action and set a flag to check if the form has been already submitted.

But looking at Magento's VarienForm I've seen there is already a hook for the submit option, so I was dubious if it's ok to reuse that action.

2

Got it working now, what I've done is using jQuery to hook on myForm.submit() action as follows:

var myForm = new VarienForm('my-form-id', false);
var formAlreadySubmitted = false;
$j('#my-form-id').submit(function(e){
   if(myForm.validator.validate()){
      if(formAlreadySubmitted){
         e.preventDefault();
            return false;
         }
         var submitChildren = $j(this).find('button[type=submit]');
         submitChildren.attr('disabled', 'disabled');
         submitChildren.addClass('disabled');
         formAlreadySubmitted = true;
    }
});

Notice that I'm only disabling the submit button if the form passes Magento validation, so to give them the chance to correct the form if it's invalid.

  • I've done something similar with the 'add to cart' button that involves ajax. I just disabled the button before I submitted the ajax request and reenabled the button when I got an ajax response. – easymoden00b Mar 26 '15 at 17:49
1

Update in Magento 2, my script in my .phtml is

<script type="text/javascript">
    require([
        "jquery"
    ], function ($) {
        var myForm = $('#inquiry-form');
        var formAlreadySubmitted = false;
        myForm.submit(function (e) {
            if (myForm.validation("isValid")) {
                if (formAlreadySubmitted) {
                    e.preventDefault();
                    return false;
                }
                var submitChildren = $(this).find('button[type=submit]');
                submitChildren.attr('disabled', 'disabled');
                submitChildren.addClass('disabled');
                formAlreadySubmitted = true;
            }
        });
    });
</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.