Note that there are some explanatory texts on larger screens.

plurals
  1. POOn Click alert if $.get returns a value, if not, submit the form
    primarykey
    data
    text
    <p>If the submit button is clicked, prevent the default action and see if the field 'account_name' is already in use. If the <code>$.get()</code> returns a result, alert the user of the results. If it doesn't, submit form with <code>id="add_account_form"</code>. </p> <p>My problem is that my else{} statement is not submitting the form. I get no response when submit is clicked &amp; there is no value returned. </p> <p>Also I would like to change my code where it goes <code>$("#add_account_form").submit(..)</code> instead of <code>.click()</code> however, would that cause a problem when trying to submit the form later in the script?</p> <pre><code> &lt;script type="text/javascript"&gt; $(document).ready( function () { $("#submit").click( function () { var account_name = $("input[name=account_name]").val(); $.get( "'.url::site("ajax/check_account_name").'", {account_name: account_name}, function(data){ if(data.length &gt; 0){ confirm( "The account name you entered looks like the following:\n" +data+ "Press cancel if this account already exists or ok to create it." ); }else{ $("#add_account_form").submit(); } }); return false; }); }); &lt;/script&gt; &lt;p&gt; &lt;input type="submit" id="submit" class="submit small" name="submit" value="Submit" /&gt; &lt;/p&gt; &lt;/form&gt; </code></pre> <p>Thanks for your help.</p> <h2><strong>EDIT</strong></h2> <p>So anyone who runs into my problems, it's that $.get() is asynchronous, so it will always return false, or true depending on what submitForm is defined as. $.ajax() however, allows async to be set as false, which allows the function to finish before moving on. See what I mean:</p> <pre><code> $(document).ready( function () { $("#add_account_form").submit( function () { var submitForm = true; var account_name = $("input[name=account_name]").val(); $.ajax({ type: "GET", async: false, url: "'.url::site("ajax/check_account_name").'", data: ({account_name: account_name}), success: function(data){ if(data.length &gt; 0){ if(!confirm( "The account name you entered looks like the following:\n" +data+ "Press cancel if this account already exists or ok to create it." )){ submitForm = false; } } } }); if (submitForm == false ) { return false; } }); }); </code></pre> <p>Thanks for your help @Dan</p>
    singulars
    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