1

The issue I am having is I cannot access the value of the variables outside the function. I have just started to learn jQuery and I really like it, but this has had me stuck for quite a while now.

I have read other threads related to this, but those methods still do not work!

    $.get("/new/verify.php", { username: username, email: email },
        function(data){
            var stop_loading = '0';
            if(data == 'username')
            {
                alert('username taken');
                $('#username_taken_error').show();
                $('#username').focus();
                stop_loading = '1';
            }else if(data == 'email') {
                alert('email taken');
                $('#email_taken_error').show();
                $('#email').focus();
                stop_loading = '1';
            }else if(data == 'username|email') {
                alert('username/email taken');
                $('#username_taken_error').show();
                $('#email_taken_error').show();
                $('#username').focus();
                stop_loading = '1';     
            }
    });

    alert(stop_loading);

    if(stop_loading == '1') {
        alert('Stop loading');
        return false;   
    }

2 Answers 2

2

As $.get() performs an AJAX request which is asynchronous, so within your $.get() success function call another function with your stop_loading like following:

 $.get("/new/verify.php", { username: username, email: email },
        function(data){
            var stop_loading = '0';
            if(data == 'username')
            {
                alert('username taken');
                $('#username_taken_error').show();
                $('#username').focus();
                stop_loading = '1';
            }else if(data == 'email') {
                alert('email taken');
                $('#email_taken_error').show();
                $('#email').focus();
                stop_loading = '1';
            }else if(data == 'username|email') {
                alert('username/email taken');
                $('#username_taken_error').show();
                $('#email_taken_error').show();
                $('#username').focus();
                stop_loading = '1';     
            }
            // call function with stop_loading
            callAFunction(stop_loading);
    });

    // this function will be called
    // with stop_loading arugment

    function callAFunction(stop_loading){
      if(stop_loading == '1') {
        alert('Stop loading');
        return false;   
      }
    }
Sign up to request clarification or add additional context in comments.

Comments

1

Declare your variable in parent scope:  var stop_loading = '0';

$.get("/new/verify.php", { username: username, email: email },
    function(data){

        if(data == 'username')
        {
            alert('username taken');
            $('#username_taken_error').show();
            $('#username').focus();
            stop_loading = '1';
        }else if(data == 'email') {
            alert('email taken');
            $('#email_taken_error').show();
            $('#email').focus();
            stop_loading = '1';
        }else if(data == 'username|email') {
            alert('username/email taken');
            $('#username_taken_error').show();
            $('#email_taken_error').show();
            $('#username').focus();
            stop_loading = '1';     
        }
});

alert(stop_loading);

if(stop_loading == '1') {
    alert('Stop loading');
    return false;   
}

1 Comment

Yea I tried adding the variable outside of the ready function, adding it outside the get function and of course added it inside but every time I alert the value it's null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.