Jump to content

var outside of function alternative


slj90

Recommended Posts

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>"); 
}

Link to comment
https://forums.phpfreaks.com/topic/290521-var-outside-of-function-alternative/
Share on other sites

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.