Jump to content

AJAX/Javascript Onload


Hendrick

Recommended Posts

Hey. I'm new to ajax and javascript. I'm just using ajax for a small part of this script, so, anyone see whats wrong?

<title>AJAX test</title><script language="javascript" type="text/javascript">function getPage(page){var xmlhttp=false;		try {				xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');		} catch (e) {				try {						xmlhttp = new						ActiveXObject('Microsoft.XMLHTTP');			} catch (E) {				xmlhttp = false;						}		}		if (!xmlhttp && typeof XMLHttpRequest!='undefined') {				xmlhttp = new XMLHttpRequest();		}		var file = 'read.php?page=Hendrick';	xmlhttp.open('GET', file + page, true); 	xmlhttp.onreadystatechange=function() {		if (xmlhttp.readyState==4) {				var content = xmlhttp.responseText;				if( content ){					  document.getElementById('content').innerHTML = content;				}		}		}		xmlhttp.send(null)return;}</script></head> <body><div id="links">Links</div><div id="content"> </div></body></html>

My problem is the onload. I really never worked that much with ajax/javascript, so I got help from a few people I know, and this is what we have come up with. The problem is the onload. Not really sure how to do that. When you load the page, it is blank.Basically, I'm not sure where to put the <script onload=""> And what should go inbetween the ""Thanks for the help.

Link to comment
Share on other sites

At this point in your code:

var file = 'read.php?user=Hendrick';xmlhttp.open('GET', file + page, true);

What is the value of "page"? More specifically, what is the value of "file + page" and what happens when you attempt to visit that URL by typing it into your address bar? Are you missing an & after "user=Hendrick"?

Link to comment
Share on other sites

Thanks for pointing that out. My bad. I was playing around with that, and I changed page to user, but I eventually switched it back to page. Guess I forgot one. I'll update my main post, but it should be test.php?page=Hendrick
But it still appears that you are passing a "page" parameter through your "getPage" function and then appending that value to your "file" variable so the request that you are sending through AJAX will look something like this:
"read.php?page=Hendrick" + page

Is that what you are hoping for? If so, what is the value that you are passing as "page"?Additionally, what happens if you simply type "read.php?page=Hendrick" into your address bar? Does that PHP file load correctly?

Link to comment
Share on other sites

Ya, your right on target. Right now, the script is just basic, so I'm seeing if I can retrieve data from the database, and then I'm going to see if the ajax part itself works, then from there I'm going to make it a more complicated/safer script.But, when I do go to the page directly, (the read.php page) the data is shown like it should.

Link to comment
Share on other sites

So now you need to get that getPage() function to run? You said <body onload="java script:getPage();"> didn't work, have you tried this?:

<body onload="getPage('');">

You might also, for debugging purposes, add the following to your getPage function:

var file = 'read.php?page=Hendrick';xmlhttp.open('GET', file + page, true);xmlhttp.onreadystatechange=function() {	alert("readyState = " + xmlhttp.readyState); // <--- add this for debugging	if (xmlhttp.readyState==4)	{		alert("status = " + xmlttp.status);  // <-- add this for debugging		if(xmlhttp.status == 200)	// <--- add this for good practice		{			var content = xmlhttp.responseText;			alert("content = ]" + content + "["); // <--- add this for debugging			if( content )			{				document.getElementById('content').innerHTML = content;			}		}	}}xmlhttp.send(''); // <-- send('') rather than send(null)

So, the alerts should pop up if there wasn't any problems with creating the xmlhttp object and sending off the request. If you see the alert which says "status = 200" then you know that the request was sent and the response came back ok. The next alert should show you the content. If you see "content = ][" then the response came back null and there is something going on with your PHP page. Hope this helps.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...