Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery: Submit form when AJAX finishes
    primarykey
    data
    text
    <p>So I have a form that on clicking submit, I want to do some validation, before it submits. Pretty straight forward I think. So I setup a submit handler on the form and wanted it to do the AJAX check, and when it finishes, if its all good, go ahead and submit as usual.</p> <p>I tried this, and it failed for very obvious reasons:</p> <pre><code>$.when(checkUsername()).done(function (a) { checkTitle(); checkMessage(); if (userValid &amp;&amp; titleValid &amp;&amp; messageValid) { return true; } }); return false; </code></pre> <p><code>checkUsername</code> is a <code>$.post</code>, while <code>checkTitle</code> and <code>checkMessage</code> are just checking if any characters are in those fields. Each of the functions relatively validates <code>userValid</code>, <code>titleValid</code>, and <code>messageValid</code>, setting those values true or false. The two input fields and text area field has a blur attached with each of the related functions, so even before the user clicks submit, the form is validating.</p> <p>So obviously, with the return false at the end, the form would never submit. BUT, I originally had the valid check outside the done (where the current return false is), and if the user entered 3 valid items, then erased a field and didn't leave it/blur, and directly clicked submit, it would still submit, because with the AJAX still processing, the fields hadn't undergone a second round of checking.</p> <p>Essentially, I want the form to wait on submission until the AJAX finishes. But the <code>submit()</code> function will process its return before the AJAX finishes, and I can't put it in a done, since I need the AJAX to trigger on on the submit. Essentially, a catch 22.</p> <p>I came up with something that I feel is a messy way of doing it, but the only method I can think of at the moment, and could use advice on if this is the only way to do it, or what other options I have. I am aware I could just submit the form via AJAX, but I'm hoping to see what options are available. This is the messy method:</p> <pre><code>if (validated) return true; else { $form = $(this); $.when(checkUsername()).done(function (a) { checkTitle(); checkMessage(); if (userValid &amp;&amp; titleValid &amp;&amp; messageValid) { validated = true; $form.submit(); } }); return false; } </code></pre> <p>In this case, there is a master validate variable only set by the ajax, and the system resubmits when done.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload