PHP For Life
PHP For Life

Reputation: 109

Javascript/jQuery undefined

The reason that the title is named "jQuery / Javascript undefined" isn't because that I assume jQuery is a language, I do know that jQuery is a library of javascript language, just in case if some pedantic readers read this. The reason why I wrote that as the title is because I don't know what is wrong with the code, the jQuery or the Javascript (all jQuery's code compiled) is wrong.

Okay, back to the question, take a look at the following code, they give me an undefined value

//username validation
function username_val(){
    username_value = $("input[name='username']").val();
    span_error = "span[name='username_error']";
    $(span_error).load('ajax_signup_username', {php_username_error:username_value},  function(){
        return true;
    });
}

I alerted this, and then an "undefined" is returned. I assumed that a true would be returned instead.

function final_val(){
        alert( username_val() );
}

EDIT: Some of you guys said that I can only return true on the success param, but I need this for validation, so if all of the validation methods are true, in the final_val will return true. The point is that I needed a true value in the final_val() or if you guys have other method to validate it, please tell me. Note: I'm in a hurry, so if I misunderstand your answer, please forgive me. I'll be gone for a few hours, until then I'll check your answers.

Thanks!

Upvotes: 1

Views: 104

Answers (1)

thecodeparadox
thecodeparadox

Reputation: 87083

You need to make the alert( username_val() ); within success function. As .load() is asynchronous method so it can't return true after immediate call. So you can try this:

 $(span_error).load('ajax_signup_username', {php_username_error:username_value},  function(){
     alert('Something');
     var response = true;
     final_val(response);
 });

 function final_val(response){
     alert( response);
 }

here is the doco

Upvotes: 2

Related Questions