SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    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?

  2. #2
    SitePoint Enthusiast
    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

  3. #3
    SitePoint Enthusiast
    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?

  4. #4
    SitePoint Enthusiast
    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

  5. #5
    SitePoint Enthusiast
    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.

  6. #6
    SitePoint Enthusiast
    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.

  7. #7
    SitePoint Enthusiast
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •