SitePoint Sponsor |
|
User Tag List
Results 1 to 7 of 7
Thread: Help with AJAX concept
-
Aug 6, 2007, 21:57 #1
- Join Date
- Oct 2005
- Posts
- 98
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Help with AJAX concept
I've done the post and get method of AJAX requests. Usually you get the response and insert it into the corresponding place on the page. But how would I go about getting two sets of data? I need to get the main response, but also a value that the user doesn't see that is used to control another script. So is there a way to receive two values with one response? Or would I need to concatenate both together and separate them again when I get the response?
-
Aug 6, 2007, 22:30 #2
- Join Date
- Aug 2005
- Location
- Perth, Australia
- Posts
- 27
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I'm assuming you're using a text based response. Try using an xml based response. This gives you the ability to receive numerous records.
more basic introduction info at http://www.w3schools.com/ajax/default.asp
-
Aug 6, 2007, 23:04 #3
- Join Date
- Oct 2005
- Posts
- 98
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Just for clarification, so if I do put it in an xml response, can I grab the number response and just show the text?
-
Aug 6, 2007, 23:52 #4
- Join Date
- Aug 2005
- Location
- Perth, Australia
- Posts
- 27
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
yes, but make sure you use responseXML rather than responseText, that way the javascript knows to treat the response as a xml rather than text.
check the page http://www.w3schools.com/ajax/ajax_responsexml.asp for a good example
-
Aug 7, 2007, 14:41 #5
- Join Date
- Oct 2005
- Posts
- 98
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Okay, I get the xml way, except for one problem. Didn't realize it till after. I'm sending the article number and page number to the script. I'm receiving the content of the page and the height of the div the article will be in. I see how to send those, but the problem is, to get the height of he div I need to use javascript to get the height of it. But to get the height of it the div has to be displayed. So the response would be the div, then the xml response. That will mess it up right? If you're not sure what I'm getting at, I'll try to post a mock code.
-
Aug 7, 2007, 19:00 #6
- Join Date
- Oct 2005
- Posts
- 98
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
New Problem
Okay, I'm having trouble with AJAX period right now.
Here is the html:HTML Code:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> <script type="text/javascript" src="functions.js"></script> </head> <body> <div id="article"> Just a test asdhf jasldkjh askjdfahl dfja df hkdhkjsh sdh fkhsdk fjksdhf jhasdfhsakjdg hjdhfhsd ljsdhfsdahf ksdjfh skajd </div> <a href="#" onclick="cLoad(1, 2);return false;">Click</a> </body> </html>
Code JavaScript:var request = null; function createRequest() { try { request = new XMLHttpRequest(); } catch (trymicrosoft) { try { request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (othermicrosoft) { try { request = new ActiveXObjext("Microsoft.XMLHTTP"); } catch (failed) { request = null; } } } if (request == null) alert("Error creating request object!"); } function cLoad(articleID, pageNum) { //mySlider.toggle(); createRequest(); var url = "getArticle.php?id="+escape(articleID)+"&page="+escape(pageNum); request.open("GET", url, true); request.onreadystatechange = updateContent; request.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); request.send(null); } function updateContent() { if(request.readyState == 4) { if(request.status = 200) { var response = "Loading"; document.getByElementID("article").innerHTML = response; //mySlider.toggle(); } else { var response = "Something is messed up!"; document.getByElementID("article").innerHTML = response; } } }
I'm just testing to see the if AJAX is even working, but nothing happens. If I put an alert in the createRequest it alerts that a request was created, so I know that is firing. But seems to stop there. I even tried making cLoad just change the innerHTML value and it won't even do that. Any help would be appreciated. Also the php file by itself creates the required data and echo's it back.
-
Aug 7, 2007, 22:32 #7
- Join Date
- Oct 2005
- Posts
- 98
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Never mind I got it working mod-rewrite was screwing up my url variable and I had some small typos with the innerHTML statement.
Bookmarks