-1

I could reject the form by checking either the CSRF token or by adding some other token to my DB and check if that was already used, but I was wondering if there seems to be an easier way to handle this with javascript (using jquery would be acceptable)?

I want to avoid concurrency of two/three event submissions. Disabling the button/form with on click and submit events did not work.

Example:

$("#request-form").submit(function() {
   $(this).attr('disable', true);
});
$('#btn_modal_submit').on('click', function() {
   $(this).attr('disable', true);
});
Brian Mains
  • 50,520
  • 35
  • 148
  • 257
renno
  • 2,659
  • 2
  • 27
  • 58

2 Answers2

1

The attribute needed is disabled, not disable, and it's OK to target the current object - but you may want to consider disabling all of the buttons on the form like so (assuming they are all "button" type):

$("#request-form").submit(function() {
   $("button").attr("disabled", "disabled");
});
$('#btn_modal_submit').on('click', function() {
   $("button").attr("disabled", "disabled");
});

Or using the prop("disabled", true) function. For additional reference: https://stackoverflow.com/a/6048113/231716

I targeted the button control directly which would indicate to disable all buttons and prevent accidental clicking until the correct form posts back. You probably don't need to attach to both form submit and button click, but that will depend on the behavior of the application. Also, if not a true postback, such as an AJAX postback, you can combine that with window.setTimeout to re-enable the button.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
  • put an alert("HERE") in each to verify these are being called in the first place... – Brian Mains Nov 03 '17 at 15:34
  • I was using the label tag to submit the form, because I was using a modal... Disabling the real button/input (type="submit") made it work. thanks – renno Nov 03 '17 at 16:03
1

You could use .one() instead of .on() for submitting the form on click.

$('#btn_modal_submit').one('click', function(){
    $("#request-form").submit();
});

http://api.jquery.com/one/

Jimmy
  • 2,669
  • 1
  • 17
  • 21