SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    Oct 2005
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    AJAX- trying to output xml data problem

    I've almost completed a task where im trying to use PHP to generate an XML file which i can use to display information relating to a country from a dropdown menu.

    I've resolved the PHP XML file issue thanks to help from these boards but i am stuck on trying to access the data from the <location/> tags and other tags as well from the XML file. The code is below but currently i keep getting
    [object HTML Collection]

    http://callsabroad.gb.com

    HTML Code:
    var xmlHttp = createXmlHttpRequestObject();
    
    function createXmlHttpRequestObject()
    {
    	var xmlHttp;
    
    	try 
    	{
    		xmlHttp = new XMLHttpRequest();
    	}
    	catch(e)
    	{
    	
    		var xmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
    										"MSXML2.XMLHTTP.5.0",
    										"MSXML2.XMLHTTP.4.0",
    										"MSXML2.XMLHTTP.3.0",
    										"MSXML2.XMLHTTP",
    										"Microsoft.XMLHTTP");
    		
    		
    		for (var i=0; i<xmlHttpVersions.length && !xmlHttp; i++)
    		{
    			try
    			{
    		
    			xmlHttp =  new ActiveXObject(xmlHttpVersions[i]);
    			}
    			catch(e) {}
    		}
    	}
    		
    		
    		
    		if(!xmlHttp)
    			alert("Error Creating the XMLHttpRequest object.");
    		else
    			return xmlHttp;
    }
    
    
    function process()
    	{
    	
    		
    		if(xmlHttp)
    		{
    		
    		try
    				{
    					
    					//get values from option tag
    					var id = document.getElementById("country").value;
    					
    					//create params string
    					
    					var params = "country="+id;
    					
    					//initate the asynchronous HTTP request
    					xmlHttp.open("GET", "country-find.php?" + params, true);
    					xmlHttp.onreadystatechange = handleRequestStateChange;
    					xmlHttp.send(null);
    				}
    			
    		
    				catch(e)
    				{
    					alert("Can't connect to the server:\n" + e.toString());
    				}
    		}	
    			
    	}	
    	
    function handleRequestStateChange()
    	{
    	
    	
    		if (xmlHttp.readyState == 4)
    		{
    			
    			if (xmlHttp.status == 200)
    			{
    				try
    				{
    				  handleServerResponse();
    				}
    				catch(e)
    				{
    				
    					alert("Error reading the response: " + e.toString());
    				}
    			}
    			else
    			{
    				
    				alert("There was a problem retreiving the data:\n" + xmlHttp.statusText);
    			}
    		}
    	}
    	
    function handleServerResponse()
    	{
    	
    		
    		var xmlResponse = xmlHttp.responseXML;
    		
    		//catching potential errors with IE and Opera
    		
    		if(!xmlResponse || !xmlResponse.documentElement)
    		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
    		
    		//catching potential errors with Firefox
    		var rootNodeName = xmlResponse.documentElement.nodeName;
    		if(rootNodeName == "parsererror")
    		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
    		
    			
    		//obtain the XML documents element
    		xmlRoot = xmlResponse.documentElement;
    		
    		//testing that we received the XML document we expect
    		
    		if(rootNodeName != "response" || !xmlRoot.firstChild)
    		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
    		
    		responseText = xmlRoot.getElementsByTagName('location');
    		//display the user message
    		
    			
    			myDiv = document.getElementById("myDivElement");
    			myDiv.innerHTML = "Server says the answer is: " + responseText;
    	}
    	

  2. #2
    SitePoint Evangelist
    Join Date
    Oct 2005
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    forgot to show the XML that the PHP generates if INDIA is selected for example
    HTML Code:
    <?xml version="1.0"?>
    <response>
    <country>
    <id>1</id>
    <location>India</location>
    <content>Test Data for India</content>
    </country>
    </response>

  3. #3
    SitePoint Evangelist
    Join Date
    Oct 2005
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have been using Firebug to debug the page and have found out that my error seems to be this piece of code.

    Code:
    responseText = xmlRoot.lastChild.text
    When I use that peice of code it wont get the child value but returns null or undefined. However if i use
    Code:
    responseText = xmlRoot.lastChild.nodeName
    the tag value is returned. How can i access the text content between the <content/> tag as i thought this was the correct way?

    Cheers

    Code:
    function handleServerResponse()
    	{
    	
    		
    		var xmlResponse = xmlHttp.responseXML;
    		
    		//catching potential errors with IE and Opera
    		
    		if(!xmlResponse || !xmlResponse.documentElement)
    		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
    		
    		//catching potential errors with Firefox
    		var rootNodeName = xmlResponse.documentElement.nodeName;
    		if(rootNodeName == "parsererror")
    		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
    		
    			
    		//obtain the XML documents element
    		xmlRoot = xmlResponse.documentElement;
    		
    		//testing that we received the XML document we expect
    		
    		if(rootNodeName != "response" || !xmlRoot.firstChild)
    		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
    		
    		
    		
    		responseText = xmlRoot.lastChild.nodeName;
    		//display the user message
    		
    			
    			myDiv = document.getElementById("myDivElement");
    			myDiv.innerHTML = responseText;
    	}


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
  •