www.webdeveloper.com
Results 1 to 7 of 7

Thread: javascript return value

  1. #1
    Join Date
    Jul 2009
    Posts
    6

    javascript return value

    G'day Guys

    My issue SHOULD be pretty basic but I'm not sure whats going on.

    I found I can only RETURN a value if I return it from the "root" of the function. For example, if I type "if(x==1){ return true; }" it doesn't want to return a value becuase its within an if..

    Anyways this is my exact issue:

    HTML Code:
    <form action="login.php" onsubmit="return usercheck('username', 'password', 'login');" method="post" name="login" id="login">
    So I call usercheck() from onsubmit on my form.

    Code:
    function usercheck(user, pass, form){
    
    	var user = document.getElementById(user).value;
    	var pass = document.getElementById(pass).value;
    	
    	var queryStr = 'username=' + escape(user) + '&password=' + escape(pass); // this is a URL variable to the php //
    	var strURL = 'ajax/usercheck.php?' + queryStr;
    	
    	xmlHttpReq.open('POST', strURL, true);
    	xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');		
    	xmlHttpReq.send(null);
    	
    	xmlHttpReq.onreadystatechange =	
    	function returner(){
    
    		if(xmlHttpReq.readyState == 4){
    		
    			var response = xmlHttpReq.responseText;
    			
    			if(response == 0){					
    				alert("The information you have entered is incorrect.");
    				return false;
    			} else {
    				return true;
    			}
    		
    		}
    	  
    	}
    	
    	returner = returner();
    	alert(returner);
    	return returner;
    
    }
    The alert(returner); returns back undefined.

    If I type return true; UNDER the closing bracket of the xmlHttpReq.readyState == 4 if, then the alert box returns true...

    So very confused 4 hours AFTER I started working on this issue.

    Thanks alot to anyone that can help

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    check the status as well
    [code]
    if(xmlHttpReq.readyState == 4){
    if(xmlHttpReq.status==200){
    ...
    ...
    }
    }

  3. #3
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    You are making an asynchronous XMLHTTPRequest so it goes off and does it thing while you code keeps on going.

    It is like sending your buddy off to the store to get food while you stay home and play games. He goes and gets it while you have fun. He comes back and you get to eat.

    What you need is a synchronous call.

    If you both went, you would not be playing the games. You would have to wait to start playing when you got back.

    Eric

  4. #4
    Join Date
    Jul 2009
    Posts
    6
    I have added this (I guess for general use) but this has not changed my 'undefined' return value.

    Any help on why my returns inside the returner() function keeps returning undefined would be highly appreciated.

  5. #5
    Join Date
    Jul 2009
    Posts
    6
    Hah, alien, that just went straight over my head.

    This is really my first ajax application I have attempted to make myself... but after the 4 hours of no return I have given up and come to beg for help here!

    What I'm trying to say is you need to be very specific... If possible if you can show me snippets to edit/remove/add

  6. #6
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    You need an synchronous Ajax request. Google for it.

    Eric

  7. #7
    Join Date
    Jul 2009
    Posts
    6
    Yeh no help - People jump into the deep end thinking becuase they know it so should everyone else...

    But I copy/pasted a line of my code and Googled it and found a blog which someone actaully thought it was a good idea to explain.

    I have changed my code to this
    Code:
    	var user = document.getElementById(user).value;
    	var pass = document.getElementById(pass).value;
    	
    	var queryStr = 'username=' + escape(user) + '&password=' + escape(pass); // this is a URL variable to the php //
    	var strURL = 'ajax/usercheck.php?' + queryStr;
    	
    	xmlHttpReq.open('GET', strURL, false);
    	xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');		
    	xmlHttpReq.send(null);
    	var ajaxresponse = xmlHttpReq.responseText;
    	
    	if(ajaxresponse == 0){					
    		alert("The information you have entered is incorrect.");
    		return false;
    	} else {
    		return true;
    	}
    This now works... but it feels very dodgey... I followed the opinions of the blog writer but if it is a good way to do it or not I don't know.

    Anyone care to enlighten?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles