bmx322 Posted January 5, 2010 Share Posted January 5, 2010 Hello! I'm new to ajax but have been programming for over 6 years with PHP. The issue I am having is: I get "undefined" as the only output from my test scripts. I have two files: test.html and testimo_query.php. I've been at this for 5 hours now. I've looked at a multitude of examples and did a couple tutorial, yet I can't seem to get any data. What is wrong (not with me silly- the code LOL) Thanks to all who take the time to help! Here are the two files. test.html <html> <head> <SCRIPT language=JavaScript type="text/javascript"> var http = createRequestObject(); function createRequestObject() { // find the correct xmlHTTP, works with IE, FF and Opera var xmlhttp; try { xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { try { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { xmlhttp=null; } } if(!xmlhttp&&typeof XMLHttpRequest!="undefined") { xmlhttp=new XMLHttpRequest(); } return xmlhttp; } // Make the XMLHttpRequest object function sendRequest() { // Open PHP script for requests try{ http.open("GET", "testimo_query.php", true); http.setRequestHeader('Content-Type', "text/text"); http.onreadystatechange = handleResponse; http.send(null); } catch(e){ // caught an error alert('Request send failed.'); } } function handleResponse() { try{ if(http.readyState == 4){ var response = http.responseText.documentElement; // UPDATE ajaxTest content document.getElementById("ajaxDiv").innerHTML = response; } } catch(e){ // caught an error alert('Response failed.'); } } function repeatloop() { sendRequest(); setTimeout("repeatloop()", 10000); } window.onload=function() { repeatloop(); } </script> </head> <body> <div id='ajaxDiv'></div> </body> </html> testimo_query.php <?php include("dbconnect.php"); $sql = "select q2b,q2a from 'TABLE NAME HERE' order by id_client desc limit 1"; $rs = mysql_query($sql); while($row = mysql_fetch_array($rs)){ $fn = $row['q2b']; $ln = $row['q2a']; $response = "$fn <br />"; $response .= "$ln"; $response .= "inside TESTIMO"; } echo $response; ?> Link to comment https://forums.phpfreaks.com/topic/187208-ajax-no-errors-but-no-data-from-db/ Share on other sites More sharing options...
trq Posted January 5, 2010 Share Posted January 5, 2010 What does firebug's console show? Link to comment https://forums.phpfreaks.com/topic/187208-ajax-no-errors-but-no-data-from-db/#findComment-988607 Share on other sites More sharing options...
bmx322 Posted January 5, 2010 Author Share Posted January 5, 2010 IE 8 and FF just shows "undefined" as the only output. It seems I'm not getting any data back from the database but I'm real good with PHP and SQL, I double checked that, it seems fine. All I really did was imitate what was on the tutorials and examples as far as the PHP page is concerned. Thanks for responding. Link to comment https://forums.phpfreaks.com/topic/187208-ajax-no-errors-but-no-data-from-db/#findComment-988609 Share on other sites More sharing options...
corbin Posted January 5, 2010 Share Posted January 5, 2010 http.responseText.documentElement That property doesn't exist, hence the undefined. Just use http.responseText. (http://www.w3.org/TR/XMLHttpRequest/#text-response-entity-body) Also, why are you doing a while loop for a query with a LIMIT 1? Also, inside the query, you're setting $response over again each loop meaning you will only get the last result in it (assuming you had more than 1 result). Link to comment https://forums.phpfreaks.com/topic/187208-ajax-no-errors-but-no-data-from-db/#findComment-988692 Share on other sites More sharing options...
bmx322 Posted January 5, 2010 Author Share Posted January 5, 2010 And may I say! Thank you!! Thank you!! Thank you!! I took out the part you mentioned and all is well. As for the "Limit 1" and rewriting the $response; That was intentional during testing. I am going to expand the code to get a different record each time. Again! Thank you!! Link to comment https://forums.phpfreaks.com/topic/187208-ajax-no-errors-but-no-data-from-db/#findComment-988786 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.