Jump to content

loading data from a database using php and ajax


wrathican

Recommended Posts

Heya people.

 

I'm loading data into a page from a database using php and Ajax(more AHAH really).

 

something really weird is happening. my code works, but only if i have an alert in my get request.

if i take the alert out the data doesn't load.

 

this is my JS:

/* ---------------------------- */
/* XMLHTTPRequest Enable */
/* ---------------------------- */
function createObject() {
var ajaxRequest;  // The variable that makes Ajax possible!

try{
	// Opera 8.0+, Firefox, Safari
	ajaxRequest = new XMLHttpRequest();
} catch (e){
	// Internet Explorer Browsers
	try{
		ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try{
			ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e){
			// Something went wrong
			alert("Your browser broke!");
			return false;
		}
	}
}
return ajaxRequest;
}

var http = createObject();

function insertReply(whereTo) {
var state = http.readyState;
var response = http.responseText;
if(state == 4){
	document.getElementById(whereTo).innerHTML = response;
}else{
	document.getElementById(whereTo).innerHTML = 'Working...';
}
}

function retrieveData(action, whereTo) {
var insert = whereTo;
document.getElementById(insert).innerHTML = "Loading...";
var url = 'admin.php?';
nocache = Math.random();
var query = 'retrieve=retrieve&action='+action+ '&nocache=' + nocache;
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", query.length);
http.setRequestHeader("Connection", "close");
http.send(query);
alert(whereTo); // this is the alert needed
http.onreadystatechange = insertReply(insert);

}

 

no point in showing php since that works...

 

this is how i call the function:

retrieveData('pagelist', 'pagelist');

 

have i typed something wrong? i cant for the life in me figure this out..

 

Thanks

If the retrieveData('pagelist,'pagelist'); comes before the object that has the id of pagelist, it seems to not work.  By moving it below (in code order) the object, it seemed to work.

 

example:

<script type="text/javascript">
retrieveData('pagelist','pagelist');
</script>
<div id="pagelist"></div>

to->

<div id="pagelist"></div>
<script type="text/javascript">
retrieveData('pagelist','pagelist');
</script>

 

If that doesn't work, let me know.  This points to the page not being fully loaded at the time of the attempt, the alert would bring it around to being loaded enough to that point.

nope, sorry, i still couldn't get it to work without the alert. I had a suspicion (that really doesn't look like the spelling for that word, but it is!) that what you said may have been the case. I can't for the life in me fathom this one out.

 

I tried putting the <script></script> tags at the very bottom of the page and calling the function and I got the same thing.

 

I'm not an Ajax/JavaScript-spert but they way I see it is that it should work. :S

 

odd.

 

Thanks for your help!

Archived

This topic is now archived and is closed to further replies.

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