Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Can't get a return value from javascript function
Old 05-17-2010, 11:39 AM Can't get a return value from javascript function
Experienced Talker

Posts: 37
Trades: 0
I'm trying to use javascript to check a MYSQL database after a user tabs out of a username field so as to notify if the name is in use or not. The function code I'm using is:

PHP Code:

function checkUser(str) {
if (
str=="") {return false;}

if (
window.XMLHttpRequest) {
    
// code for IE7+, Firefox, Chrome, Opera, Safari
    
xmlhttp=new XMLHttpRequest();
    } else {
    
// code for IE6, IE5
    
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    
xmlhttp.onreadystatechange=function() {
        if (
xmlhttp.readyState==&& xmlhttp.status==200) {
            
document.getElementById("mainContent").innerHTML=xmlhttp.responseText;
        }

    }

    
xmlhttp.open("GET","getuser.php?q="+str,true);
    
xmlhttp.send();

This works, and I get the response from getuser.php placed into the mainContent DIV. However, I can not, for the life of me, get a return value. I understand the line that reads "xmlhttp.onreadystatechange=function() {blabla} and that any return values are probably returning to xmlhttp.onreadystatechange and not to the javascript that called the function "checkUser". But I can't figure out how to pull a return value. I'm calling this function like this:

PHP Code:
case "email":
        
// verify valid email address, verify if address is in use,
        // colorize accordingly. If prohibited characters are found
        // here, then set field to red and set value to ""

var emailexists showUser (field.value); 

I have tried putting return true and return false in every point of the checkUser function that I can think of but I can never read that value from the case statement - it always comes up "undefined"

Is there a good way to do this? Since I've seen that the value is being read I am considering just making a hidden div and placing the information into that DIV and reading the DIVs contents from the case statement to determine if the username was found or not. I'm sure this would work but I would really like to do it "right" using a return value.

Anyone know what I need to do?
catatung is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-17-2010, 12:51 PM Re: Can't get a return value from javascript function
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
but you only send a return value if the parameter passed to the function is an empty string.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 05-17-2010, 12:56 PM Re: Can't get a return value from javascript function
Experienced Talker

Posts: 37
Trades: 0
I took out all of my return statements after messing with it for hours... The only one I left in is that one. No matter where I put a return statement it always comes up undefined in the case statement. I read it by doing either

var something = checkUser (field.value);
alert (something);

or just

alert (checkUser (field.value));

No matter what I do it always comes up undefined.
catatung is offline
Reply With Quote
View Public Profile
 
Old 05-17-2010, 01:09 PM Re: Can't get a return value from javascript function
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Have you tried writing the if ... else statements correctly so a return value is sent if the function is successful or not?
Code:
function checkUser(str) {
if (str=="") {
return false;
} else {

if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    } else {
    // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("mainContent").innerHTML=xmlhttp.responseText;
       return = true;
        }

    }

    xmlhttp.open("GET","getuser.php?q="+str,true);
    xmlhttp.send();


}
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?

Last edited by chrishirst; 05-17-2010 at 01:11 PM..
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 05-17-2010, 01:34 PM Re: Can't get a return value from javascript function
Experienced Talker

Posts: 37
Trades: 0
I just tried return = true; where you wrote it but I got a syntax error. I had originally put in "return true;" in the same location but when I run it, it says "undefined" when i execute "alert (checkUser(field.data));

I think it is because it's a function inside a function... So the return value is going back outside the 2nd function. Maybe 2 return trues in a row? I will try that next.
catatung is offline
Reply With Quote
View Public Profile
 
Old 05-17-2010, 01:43 PM Re: Can't get a return value from javascript function
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Maybe I got the braces '{}' in the wrong order, it was an 'on the fly' edit.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to Can't get a return value from javascript function
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.23048 seconds with 12 queries