title: How To Prevent Double Submit With Form Validation
body: <p>I have been researching this problem for the last day or so.  I am trying to prevent the user from double clicking on the submit button for my django project and submitting the form twice.  I am also doing form validation so if the form validation fails, I don't want to disable the submit button.  My form validation is preventing a double submit in this case because I have a unique field in my form which is preventing the double submit.  I am also doing a redirect after the submit, but if I double or triple click the submit button the form fails and never does the redirect.</p>
  
  <p>I've tried various versions of the code below which I found on a similar SO issue...</p>
  
  <pre><code>                  $(document).ready(function (){
                       $('form').submit(function() {
                      $('#status').attr('disabled', true);
                        });
                      });
  </code></pre>
  
  <p>As an update, I've incorporated code as outlined below:</p>
  
  <pre><code>                $(document).ready(function (){
                    $("#status").click(function() {
                       if(!$("form").hasClass("submitted")){
                         $("form").addClass("submitted");
                         $("form").submit();
                       }
                     });
                   });
  </code></pre>
  
  <p>This would appear to be working...but maybe I'm missing something.  If the user clicks the button like a second after the initial submit, it tries to resubmit the form, perhaps this is as designed above?  I guess I'm trying to prevent the double click and allow the redirect to happen as it normally would if the user didn't get in the way :).  Under normal cases, this code I'm running works just fine...and redirects after the initial submit.  Is there someway to prevent the allow this to happen as expected even if the user clicks again?</p>
  
  <p>In the case of my code, I am trying to pass a value with my submit button, and as such, using the code above prevents the value from being passed when I incorporate it.  I am using HTML as shown below:</p>
  
  <pre><code>&lt;button type="submit" class="button15" name="status" id="status" value="Submitted"&gt;&lt;h3 class="txtalgn4"&gt;Submit&lt;/h3&gt;&lt;/button&gt;
  </code></pre>
  
  <p>If I use the JQuery above along with the HTML shown, they seem to conflict and it still doesn't prevent the submit.  </p>
  
  <p>Thanks in advance for any ideas on how I can incorporate form validation and preventing the user from double clicking the form and submitting it twice.  I have also seen some examples of using session specific variables, but was hoping to solve using JQuery/Javascript if possible.  </p>
  
  <p>I just tested the code below, and it appears to be executing, but the form is not getting submitted.  No errors either.  </p>
  
  <pre><code>                $(document).ready(function (){
                    $("#status73").click(function() {
                       if(!$("form").hasClass("submitted")){
                         console.log("hello world");
                         $("form").addClass("submitted");
                         $(window).unbind('beforeunload');
                         $("form").submit();
                       }
                     });
                   });
  </code></pre>
  
  <p>The hello world was to see if it is executing and in fact it is.  However, my django form is not being processed.  If I use the code above with this HTML...</p>
  
  <pre><code>&lt;button type="button" class="button15" name="status" value="Submitted" id="status73"&gt;&lt;h3 class="txtalgn4"&gt;Submit&lt;/h3&gt;&lt;/button&gt;
  </code></pre>
  
  <p>The form does not get submitted.  If I used the code above with this HTML, the form does get submitted....</p>
  
  <pre><code>&lt;button type="submit" class="button15" name="status" value="Submitted" id="status73"&gt;&lt;h3 class="txtalgn4"&gt;Submit&lt;/h3&gt;&lt;/button&gt;
  </code></pre>
  
  <p>The difference is the button type.  Again I'm new so I'm learning as I go.  Thanks for the input.</p>
  
  <p>I'm fairly certain the issue is that I'm not passing the value of Submitted when the button is changed to type button from submit.  Is there some way I can submit the value as hidden when the user clicks on the submit button?  I tried something like...</p>
  
  <pre><code>&lt;input type="hidden" name="status" value="Submitted"/&gt;
  &lt;button type="button" class="button15" name="status" value="Submitted" id="status73"&gt;&lt;h3 class="txtalgn4"&gt;Submit&lt;/h3&gt;&lt;/button&gt;
  </code></pre>
  
  <p>But this doesn't seem to pass the status value to the form.  Nothing happens.</p>
  
  <p>I just checked my console log and I have the following errors:</p>
  
  <pre><code>Uncaught ReferenceError: checkForm is not defined
      at HTMLFormElement.onsubmit ((index):104)
      at Object.trigger (jquery.min.js:2)
      at HTMLFormElement.&lt;anonymous&gt; (jquery.min.js:2)
      at Function.each (jquery.min.js:2)
      at w.fn.init.each (jquery.min.js:2)
      at w.fn.init.trigger (jquery.min.js:2)
      at w.fn.init.w.fn.(:8000/book/author/anonymous function) [as submit] (http://127.0.0.1:8000/static/jquery/jquery.min.js:2:85786)
      at HTMLButtonElement.&lt;anonymous&gt; ((index):23)
      at HTMLButtonElement.dispatch (jquery.min.js:2)
      at HTMLButtonElement.y.handle (jquery.min.js:2)
  </code></pre>
  
  <p>I also played around a bit with the hidden values.  Exploring errors above.</p>
  
  <p>I had a leftover onsubmit action in my HTML from previous attempts that was causing most of my grief.  I also changed the status field to be hidden on the form instead of in the HTML element.  Thanks for all of the help!!!!!!</p>