| Home | Register | FAQ | Members List | Search | Today's Posts | Mark Forums Read |
|
|
#1 (permalink) |
|
shiro
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,524
|
Javascript not returning true
I have a function that basically works down to this: Code:
I am using this function in the following context: Code:
Now the problem I have is that the function (someFunction) is returning true, but the 'if' (in the second piece of code I posted) statement is receiving a value of "undefined" from the function. I know for a fact the first function is returning true because I tested it in the following manner: Code:
and the result was that I got an alert window that said "before true", and no other alert windows. So the function is definitely returning, and I am under the impression that it should be a returning a value of true, yet for some reason the 'if' statement is not receiving that value from the function Has anybody else ever encountered this before? Is it a syntax error somewhere? |
|
|
|
|
|
#4 (permalink) |
|
shiro
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,524
|
Here are the actual functions I am using (with unnecessary stuff removed) function: Code:
And the if statement that calls it: Code:
with 'alert(validateEmailAddress("email"))' I get 'undefined, and with 'alert(proceed)' I get 'false' |
|
|
|
#5 (permalink) |
|
shiro
Join Date: Aug 2007
Location: Yokohama, Japan
Posts: 2,524
|
I figured it out. Kind of hard to explain the problem, but I'll try so that others can avoid this problem in the future, as it was a fairly big one. This is how I had the structure of my script set up: 1) set an onclick listener on the submit button of my form 2) when the submit button is clicked, javascript validated the fields in the form, one of which was an email field. Two different things were checked in the email field: 2a) is there a value in the field? 2b) is the email address entered into the field a valid email address? This second check was done using an AJAX call to a PHP function that checks to see if the domain name in the email address exists. If it doesn't, a value of false was returned to the onclick listener for the submit button This is where the problem was occurring - a value of undefined was being received by the onclick listener even though the value being returned was true (see my above scripts). I finally realized the problem wasn't in my return value when I tested the script with a non-existent email address (haku@haku.haku) and still got a value of undefined. The problem is in the structure of AJAX requests. Ajax requests are designed to be executed IF the readystate is 4 and the status is 200, rather than when this happens. As a result, when the script came to this point, these conditions had not yet been met, and so the script skipped past the if statement and came to the end of the function. No value was returned at the end of the function, and so the onclick listener received a value of undefined. I suppose a value was eventually returned, but by this time the onclick listener wasn't listening for it. The solution was to start off the form validation check with the ajax request, and to include the function that called the rest of the form validation inside the ajax request. This way the form validation waits until the correct readystate and status have been reached before progressing. What a freakin headache that was! Just cost me a whole morning, but its a mistake I will not make again in the future, and hopefully none of you will now either! |
|
|
|
#10 (permalink) | |
|
Senior Member
|
Quote:
Code:
|
|
|
![]() |