Jump to content

Comparing responseText to string


ShaolinF

Recommended Posts

Hi Guys,

 

I've been trying to do this in several different occasions but it doesn't work. See the code below:

 

php script:

if ($_GET['tn'] > $result['TicketsOnSale']) {
    echo "inline";
} else {
    echo "none";
}

 

AJAX:

if(ihr.responseText == 'inline')

 

If the script echos inline, then the AJAX IF statement should be true. But even if logically it is true it still doesn't work. I've tried single and double quotes for both the php and AJAX script to no avail. The whole script works fine up until the IF statement. I've tried to debug using firebug but it outputs no errors. Why does this happen ?

Link to comment
Share on other sites

Did you try something like this:

 

<script language="javascript">

// Your AJAX Would Be Here

function check4It() {
var what = ihr.responseText;
if (what == "inline") {
// do something
}
setTimeout("check4It()", 1000);
}
window.onload = function() {
check4It();
}
</script>

 

I will not guarantee this will work; but it might be worth a shot.

Link to comment
Share on other sites

yer php script sez

if ($_GET['tn'] > $result['TicketsOnSale']) {

 

yet in a string comparison it will be either == or !=

not > and <

if ya want to use < or >  use strcmp function

 

otherwise make shure 'tn' is a number as define it as so.

 

 

 

 

But that shouldn't be a problem because I am echo-ing a string.

 

Check out my code below and tell me if you see any problems:

 

var tickno = document.signup.ticketno.value;
var queryString = "?tn=" + tickno;
xhr.open("GET","tickno.php" + queryString,true);

xhr.onreadystatechange=function() {   
if(xhr.readyState==4) {
	var what = xhr.responseText;
		if (what == "none") {
		alert("yes");
		}
}	
}
xhr.send(null);  
}

 

PHP:

<?php	
include('db_connect.php');
$result = mysql_query('SELECT TicketsOnSale FROM event WHERE eventset = 1 LIMIT 1') or die(mysql_error());
$result = mysql_fetch_array($result);

if ($_GET['tn'] > $result['TicketsOnSale']) {
    echo "setdisplay";
} else {
    echo "setnone";
}
?>

Link to comment
Share on other sites

  • 5 years later...

ShaolinF, I'm having the exact same problem with a site that I'm currently building. But on a previous site I built that was running PHP on a Windows IIS 7 server, this code works! So maybe it's a server-related issue? Does anyone else have an idea as to how we can solve this problem on a Linux server?

Link to comment
Share on other sites

ShaolinF, I hardly ever post anything in a forum, but wouldn't you know that two seconds after I post something (like my last reply), I FIGURED OUT THE PROBLEM!!!...

 

By reading this post, I learned how to use the trim() function and also the console.log(someObjectName) function. I have no idea why, but my responseText was being sent back with an extra space at the first. I HAVE NO IDEA WHY. I double-checked my PHP file that was sending the text back to my AJAX code, and it had no extra spaces... who knows!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.