1

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!

1
  • Declare your variables with var. Also, using the "name" attribute on a <span> is incorrect. Give it an "id" or "class" value instead, and use that to find it. Commented May 25, 2012 at 10:20

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

Still gives me undefined, how can I return a true value on that method ( username_val() ) ?
@PHPForLife you can try my update, I mentioned earlier that you can catch the response as you want.
Dang, I've just created my workaround for this, should have seen your response first. So this is the proper way to do it. I'll be sure to ask in SO first next time before making my workaround, thanks!

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.