Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

function problem 1

Status
Not open for further replies.

MJB3K

Programmer
Joined
Jul 16, 2004
Messages
524
Location
GB
Hi, i have this code and for some reason, it never executes the second if statement (session). Can someone tell me why? It does return the action as session too.

You can see the script in action here: click login button.
Code:
	function handleResponse() { 
		
		var ans;
		
	   	if(http.readyState == 4 && http.status == 200){ 
	
		  // Text returned FROM the PHP script 
		  	var response = http.responseText; 
			var result = eval(http.responseText.split("||"));
			
			var action = result[0];
			var retdata = result[1];
			//alert(action);
			
			  if(response) { 
				 // UPDATE ajaxTest content 
				 
				//var ret = substr(response,0,1);
				
				//if (ret == "Y"){
				// 	document.getElementById("loginStatus").innerHTML = "Login Successful";
				//}else{
				//	document.getElementById("loginStatus").innerHTML = "Login Failed";
				//}
				if(action == "login"){	
					if(retdata == "Y"){
						ans = "Login Successful";
						document.getElementById("loginStatus").innerHTML = ans;
						action = "";
						setTimeout("showUpdate()",1000);
					}else{
						ans = "Login Failed";
						document.getElementById("loginStatus").innerHTML = ans;
						action = "";
					}
				}
				if(action == "session"){
					alert("lol " + retdata);
					document.getElementById("loggedUser").innerHTML = retdata;
					action = "";
				} //else{
				//	alert("dno");
				//	action = "";
				//}
						 
		 	 } 
	
	   } 
	
	}
Any Ideas??

Regards,

Martin

Computing Help And Info:
 
>[tt]var result = eval(http.responseText.split("||"));[/tt]
Why do you need to eval()? What is wrong with this?
[tt]var result = http.responseText.split("||");[/tt]

>it never executes the second if statement (session)
You have to tell us rather. What if action is never "session"? then you do not expect it to execute or do you?
 
Does it work when action == "login"? I assume that you have tried it, shown it does, and tried it again with action == "session"... yes?

What is your progress?

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
Hi, it does return the action of session, ul see the alert boxes with the action in.

The action of "login" works because it then executes the showUpdate(); function which changes the status text.

Notice it pops up an alert after the "Construcing Session data..." status has changed to "Done!"

Regards,

Martin

Computing Help And Info:
 
the problem is, the action doesnt return "login" for the second attempt, it returns the data instead??

Login: (can be either depending on login result)

login||Y
login||N

Session:

session||Logged in as: " << that is what it splits

it returns the Logged in as bit, not the session bit in the JS?

Regards,

Martin

Computing Help And Info:
 
My guess is that it's not returning "session" but "session " (notice the space afterward) or some variation thereof.

Double-check that.

Also, have you dropped the eval() function as tsuji suggested?

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
yea, i dropped the eval() function.

Just wondering why it is returning the second split bit for the action data, not just session??

Do you want the whole script??

Regards,

Martin

Computing Help And Info:
 
Oh, so action never receives the value of "session"? I thought you said you confirmed it had by using an alert message.

Or, did it get it, but somehow it changed by the time the if-statement rolled around.

Yes, it sounds like the problem might be in something we can't see. Include all your script.

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
Here it is:
Code:
function createRequestObject() { 

   var req; 

   if(window.XMLHttpRequest){ 
	  // Firefox, Safari, Opera... 
	  req = new XMLHttpRequest(); 
   } else if(window.ActiveXObject) { 
	  // Internet Explorer 5+ 
	  req = new ActiveXObject("Microsoft.XMLHTTP"); 
   } else { 
	  // There is an error creating the object, 
	  // just as an old browser is being used. 
	  alert('Problem creating the XMLHttpRequest object'); 
   } 

   return req; 

}

//disable the form fields
function disablefields(){
	//document.getElementById('user').disabled = true;
	//document.getElementById('pass').disabled = true;
	document.getElementById('submit').disabled = true;
} 

function showUpdate(){
	document.getElementById("buildStatus").innerHTML = "Building Layout... Please Wait...";
	
	//clearTimeout(to);
	
	//build layout for logged in users
		
	setTimeout("sessionData()",2000);

}

function sessionData(){
	//document.getElementById("layoutStatus").innerHTML = "User Panel... Done!";
	document.getElementById("sessionStatus").innerHTML = "Done!";
	document.getElementById("userpanelStatus").innerHTML = "Working";

	//get session data
	sendRequest('session');

}

function userPanel(){
	//document.getElementById("layoutStatus").innerHTML = "User Panel... Done!";
	//document.getElementById("sessionStatus").innerHTML = "Done!";
	document.getElementById("userpanelStatus").innerHTML = "Done!";
	
	//setTimeout("userPanel()",2000);
}

// Make the XMLHttpRequest object 
var http = createRequestObject(); 

function sendRequest(act) { 
	
	//alert(act);
	
	if(act == "login"){
		disablefields();
		
		var up;
		
		var username = document.getElementById('user').value;
		var password = document.getElementById('pass').value;
		
		var up = "user="+username+"&pass="+password;
		
		document.getElementById("loginStatus").innerHTML = "Processing...";
		
	   // Open PHP script for requests 
	   http.open('post', 'login4.php?act='+act+"&"+up); 
	   http.onreadystatechange = handleResponse; 
	   http.send(null); 
	}
	if(act == "session"){
		//alert("Doing session");
	   // Open PHP script for requests 
	   http.open('post', 'login4.php?act='+act); 
	   http.onreadystatechange = handleResponse; 
	   http.send(null); 
		
	}

} 

function handleResponse() { 
	
	var ans;
	
	if(http.readyState == 4 && http.status == 200){ 

	  // Text returned FROM the PHP script
	    response = "";
		var response = http.responseText; 
		//var result = eval(http.responseText.split("||"));
		var result = http.responseText.split("||");
		
		var action = result[0];
		var retdata = result[1];
		alert("action: " + action);
		
		  if(response) { 
			 // UPDATE ajaxTest content 
			 
			//var ret = substr(response,0,1);
			
			//if (ret == "Y"){
			// 	document.getElementById("loginStatus").innerHTML = "Login Successful";
			//}else{
			//	document.getElementById("loginStatus").innerHTML = "Login Failed";
			//}
			if(action == "login"){	
				if(retdata == "Y"){
					ans = "Login Successful";
					document.getElementById("loginStatus").innerHTML = ans;
					action = "";
					setTimeout("showUpdate()",1000);
				}else{
					ans = "Login Failed";
					document.getElementById("loginStatus").innerHTML = ans;
					action = "";
				}
			}
			if(action == "session"){
				alert("lol " + retdata);
				alert("lol yea");
				document.getElementById("loggedUser").innerHTML = retdata;
				
				//set up the userpanel
				setTimeout("userPanel()",2000);
				
				action = "";
			} //else{
			//	alert("dno");
			//	action = "";
			//}
					 
		 } 

   } 

}

Regards,

Martin

Computing Help And Info:
 
Okay,

explain again what is happening?

What is the response and result[0] and result[1] following:

Code:
   var response = http.responseText; 
   var result = http.responseText.split("||");
   var action = result[0];
   var retdata = result[1];


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
it works fine for the login, but when it goes to the session one, it returns the 2nd part of the split, not the "session" bit which it needs to execute if(action == "session")...

For the login, it returns either login||Y or login||N

Regards,

Martin

Computing Help And Info:
 
It seems as if the http.responseText is not what you think when it "should" be session||Logged in as....

I'm still seeking a complete answer to my question, however:

me said:
What is the [red]response[/red] and result[0] and result[1] following:

Code:
   var response = http.responseText; 
   var result = http.responseText.split("||");
   var action = result[0];
   var retdata = result[1];

An answer of the following form would be nice:

you (future) said:
Using alert messages, I have discerned that when:
response = <value to be determined>
then:
result[0] = <value to be determined>
result[1] = <value to be determined>


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top