slj90 Posted August 18, 2014 Share Posted August 18, 2014 (edited) I am validating a registration form. If there is an error on the form it adds 1 onto the variable 'iserror'. The form will only submit if there are 0 errors. However, I have a function within this function that checks the database to see if the username is available or taken. When 1 is added to the variable within that function it doesn't work. After researching it a bit I have come to the conclusion it is not possible to use the variable within another function. So what could I do to validate this function as well? Thanks function buttonFunction(){ var iserror = 0; v=$("#txtusername"); $.post('../action/checkusername.php',{user:v.val().toLowerCase()},function(d){ if(d=='available'){ $("#usernamemessage").html("<span style='color:green;'>Username is available</span>"); }else if(d=='not-available'){ $("#usernamemessage").html("<span style='color:red;'>Username is not available</span>"); iserror = iserror + 1; } }); if(document.getElementById('selaccounttype').value == "AccountType") { $("#accounttypemessage").html("<span style='color:red;'>Select Account Type</span>"); iserror = iserror + 1; } else { $("#accounttypemessage").html("<span></span>"); } Edited August 18, 2014 by slj90 Quote Link to comment https://forums.phpfreaks.com/topic/290521-var-outside-of-function-alternative/ Share on other sites More sharing options...
Solution requinix Posted August 18, 2014 Solution Share Posted August 18, 2014 After researching it a bit I have come to the conclusion it is not possible to use the variable within another function.That's not it. The problem is that the inner function is executing after the rest of the outer function has completed. It's asynchronous. The functions will need to be restructured, probably such that the inner function submits the form (which the outer function used to do, I assume) and the outer function only validates. What's the rest of the code? Quote Link to comment https://forums.phpfreaks.com/topic/290521-var-outside-of-function-alternative/#findComment-1488181 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.