Note that there are some explanatory texts on larger screens.

plurals
  1. POsynchronous jquery ajax call in magento
    primarykey
    data
    text
    <p>I have a form where users are entering account numbers and I need to check that the entered account numbers are unique when the save button is clicked, but before form submission. This is on the magento backend and the ajax call is pointing to the function <code>acctnumAction()</code> in the indexController.</p> <p>The number of account numbers is dynamic, there could be up to 5, but as few as 1, so I need to do an each loop on the items. I gave the account number fields their own class and loop over that.</p> <p>This is what I've got.</p> <pre><code> function acctCheck(){ jQ.each(jQ(".acctnum"), function(x, y){ var acct = jQ(this).val(); var field = this; jQ.get("'.Mage::helper('core/url')-&gt;getHomeUrl().'dealerlocator/index/acctnum", {acctnum: acct}, function(data){ var items = JSON.parse(data); if(items != ""){ $(field).addClass("validation-failed"); return false; } }); }); } function saveAndContinueEdit(){ if(acctCheck() !== false){ editForm.submit(); } } </code></pre> <p>The save button onclick hits <code>saveAndContinueEdit()</code> and runs the function to check the account numbers.</p> <p>I know this won't work, the ajax will run asynchronously and return nothing before it could return false, and as long as it isn't false, it submits. But I have tried wrapping the ajax call in $.when to no avail, i wrapped the if in <code>saveAndContinueEdit()</code> in a $.when, like so</p> <pre><code> function saveAndContinueEdit(){ jQ("#continueEdit").val("1"); jQ.when(acctCheck()).done(function(valid){ if(valid !== false){ editForm.submit(); } }); } </code></pre> <p>but that didnt do the trick either, and is probably not even how it's supposed to work, I was just trying whatever I could come up with.</p> <p>The point of having it in the each is so that I could set a variable to false if an account number isnt valid, but still iterate through the rest so that the class will still be added to the fields if they are invalid, so the user will know, instead of fixing one then another pops up as invalid, even though my examples aren't coded that way. I know the data that is returned by the get is valid, it just won't run synchronously.</p> <p>Anyone have any ideas?</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.
 

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