954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Use Ajax responseText for if-condition

Hello,

I have a problem with the response text of an ajax request.

I want to use the answer of the php script ("true" or "false") to do different things with a if-condition, but it always takes the false option.
Here is the code.

var hr = new XMLHttpRequest();
...
var data = hr.responseText;
document.getElementById("status").innerHTML = data + "  " + data.length + "    ";

if (data != "true"){
	document.getElementById("status").innerHTML = "yes";
} else {
	document.getElementById("status").innerHTML = "no";
}


When using the data.length method, it says the response text has 306 letters (!!).

Just for testing I simplified the php script to the following:

<?php
echo "true";


It's still 303 letters.

Where is the problem? Where do all the letters come from?

Thanks you for all :)

trickist17
Light Poster
26 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

This

if (data != "true")


shoud be:

if (data == "true")
AleMonteiro
Junior Poster
164 posts since Aug 2010
Reputation Points: 15
Solved Threads: 30
 

I know, put the wrong php code up to here, but it doesn't works with both.

trickist17
Light Poster
26 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

when you print the data, what does it show?

AleMonteiro
Junior Poster
164 posts since Aug 2010
Reputation Points: 15
Solved Threads: 30
 

Here's my full code:

function startlogin(one, two)	{
var hr = new XMLHttpRequest();
var url = /*myurl*/;
var fn = document.getElementById(one).value;
var ln = document.getElementById(two).value;
var vars = "startuser="+fn+"&startpassword="+ln;
hr.open("POST", url, true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var data = hr.responseText;
if (data != "true"){ // or false...
	document.getElementById("status").innerHTML = "yes";
} else {
	document.getElementById("status").innerHTML = "no";
}
}}
hr.send(vars);
document.getElementById("startloading2").style.display = "block";
}


The thing is the length of hr.responsetext! What do you think about that?

trickist17
Light Poster
26 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

what do you mean? The length should be 4, isn't it?

And your code remains wrong: data != "true"

AleMonteiro
Junior Poster
164 posts since Aug 2010
Reputation Points: 15
Solved Threads: 30
 

It should, but it does not.

See this picture. Very simple code:
http://dl.dropbox.com/u/21806759/t.png

trickist17
Light Poster
26 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

try alerting the data and see that it says:

alert(data);

AleMonteiro
Junior Poster
164 posts since Aug 2010
Reputation Points: 15
Solved Threads: 30
 

Oh man, my hoster puts his analytics code there!
Any idea of removing this?

trickist17
Light Poster
26 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

what do u mean? show me the result.

AleMonteiro
Junior Poster
164 posts since Aug 2010
Reputation Points: 15
Solved Threads: 30
 
trickist17
Light Poster
26 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

That's sucks... the best option is to disable this functionality on the pages you need.Talk to your host provider to know how you can do it.

But you gotta other options too, one of them is to ignore that text, use replace or something like that.

AleMonteiro
Junior Poster
164 posts since Aug 2010
Reputation Points: 15
Solved Threads: 30
 
trickist17
Light Poster
26 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Replace is a good idea...

trickist17
Light Poster
26 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

You can make something like this:

data = data.substring(data.lastIndexOf('-->') + 4);
AleMonteiro
Junior Poster
164 posts since Aug 2010
Reputation Points: 15
Solved Threads: 30
 

Bad mistake. Thanks man.

trickist17
Light Poster
26 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

No problem, u r welcome.

Just mark as solved please.

AleMonteiro
Junior Poster
164 posts since Aug 2010
Reputation Points: 15
Solved Threads: 30
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: