ShaolinF Posted January 26, 2008 Share Posted January 26, 2008 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/87934-comparing-responsetext-to-string/ Share on other sites More sharing options...
phpQuestioner Posted January 27, 2008 Share Posted January 27, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/87934-comparing-responsetext-to-string/#findComment-450181 Share on other sites More sharing options...
ShaolinF Posted January 29, 2008 Author Share Posted January 29, 2008 Yes, I have tried the code upto setTimeout(). Quote Link to comment https://forums.phpfreaks.com/topic/87934-comparing-responsetext-to-string/#findComment-451982 Share on other sites More sharing options...
priti Posted January 29, 2008 Share Posted January 29, 2008 Hi, do alert and find what you are getting in responseText then trim off the extra spaces before comparing and check weather it works. Regards, Quote Link to comment https://forums.phpfreaks.com/topic/87934-comparing-responsetext-to-string/#findComment-451988 Share on other sites More sharing options...
ShaolinF Posted January 29, 2008 Author Share Posted January 29, 2008 Hi, Alert gives me the exact answer that is being compared in the IF statement. Trimming doesnt work either. Quote Link to comment https://forums.phpfreaks.com/topic/87934-comparing-responsetext-to-string/#findComment-451991 Share on other sites More sharing options...
priti Posted January 29, 2008 Share Posted January 29, 2008 Kindly check once in error console in FF .Any error is coming there or not??? check the variables names . if you can post the code we can help you more. Quote Link to comment https://forums.phpfreaks.com/topic/87934-comparing-responsetext-to-string/#findComment-451993 Share on other sites More sharing options...
laffin Posted January 29, 2008 Share Posted January 29, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/87934-comparing-responsetext-to-string/#findComment-451996 Share on other sites More sharing options...
ShaolinF Posted January 29, 2008 Author Share Posted January 29, 2008 how would i use strcmp and where? Quote Link to comment https://forums.phpfreaks.com/topic/87934-comparing-responsetext-to-string/#findComment-452048 Share on other sites More sharing options...
ShaolinF Posted January 29, 2008 Author Share Posted January 29, 2008 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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/87934-comparing-responsetext-to-string/#findComment-452734 Share on other sites More sharing options...
ShaolinF Posted January 29, 2008 Author Share Posted January 29, 2008 Damn, can't edit script. in the AJAX, change the following line: if (what == "none") { --to-- if (what == "setnone") { Quote Link to comment https://forums.phpfreaks.com/topic/87934-comparing-responsetext-to-string/#findComment-452738 Share on other sites More sharing options...
back2school4good Posted June 8, 2013 Share Posted June 8, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/87934-comparing-responsetext-to-string/#findComment-1434803 Share on other sites More sharing options...
back2school4good Posted June 8, 2013 Share Posted June 8, 2013 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! Quote Link to comment https://forums.phpfreaks.com/topic/87934-comparing-responsetext-to-string/#findComment-1434804 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.