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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

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.