views:

424

answers:

2

I have an form that takes a little while for the server to process. I need to ensure that the user waits and does not attempt to resubmit the form by clicking the button again. I tried using the following jQuery code:

<script type="text/javascript">
$(document).ready(function(){
    $("form#my_form").submit(function(){
      $('input').attr('disabled','disabled');
      $('a').attr('disabled','disabled');
       return true;
        })
    });
</script>

When I try this in Firefox everything gets disabled but the form is not submitted with any of the POST data it is supposed to include. I can't use jQuery to submit the form because I need the button to be submitted with the form as there are multiple submit buttons and I determine which was used by which one's value is included in the POST. I need the form to be submitted as it usually is and I need to disable everything right after that happens.

Thanks!

+2  A: 

...but the form is not submitted with any of the POST data it is supposed to include.

Correct. Disabled form element names/values will not be sent to the server. You should set them as readonly elements.

Also, anchors cannot be disabled like that. You will need to either remove their HREFs (not recommended) or prevent their default behaviour (better way), e.g.:

<script type="text/javascript">
$(document).ready(function(){
    $("form#my_form").submit(function(){
      $('input').attr('readonly', true);
      $('input[type=submit]').attr("disabled", "disabled");
      $('a').unbind("click").click(function(e) {
          e.preventDefault();
          // or return false;
      });
    });
</script>
karim79
Can the readonly input submit buttons be clicked on?
Adam
@Adam, yes, any attached click handlers will fire. I thought you just wanted to stop them from being modified?
karim79
@karim79 I don't care if the click handlers get fired, I just want to make sure that the form doesn't get resubmitted by the user clicking another submit button.
Adam
@Adam - no, that won't happen. I my example, I've added `$('input[type=submit]').attr("disabled", "disabled");` but my favourite way is to put `onclick="this.disabled = 'disabled'"` into the submit's onclick.
karim79
@karim79 I tried that, but then the submit button I'm clicking is not included in the POST. I need it to be included in the POST to determine which button was clicked upon
Adam
I know that you just unbided the click event for the 'a' but why not just use the $('a').click(function(e) { e.preventDefault(); // or return false; });?
fabio
@fabio - because otherwise any other click handlers attached to them will fire first, which might be an unwanted side-effect.
karim79
+4  A: 

Disable them after 50 ms or so, so that the browser has all the time to submit the form with all necessary parameters.

<script type="text/javascript">
    $(document).ready(function(){
        $("form#my_form").submit(function(){
            setTimeout(function() {
                $('input').attr('disabled', 'disabled');
                $('a').attr('disabled', 'disabled');
            }, 50);
        })
    });
</script>
BalusC
That works! Form was submitted with all parameters including the button and I couldn't submit the form twice.
Adam
You're welcome.
BalusC