Jump to content

AJAX-No errors but no data from DB


bmx322

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
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.