Jump to content

var outside of function alternative


Go to solution Solved by requinix,

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

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

  • Solution

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.