Results 1 to 3 of 3

Thread: XML DOM Question - Keep Getting Null Value

  1. #1
    Join Date
    Jun 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default XML DOM Question - Keep Getting Null Value

    I've been working on this for ages but i am trying to output some XML using Javascript. The code i got below i thought would output the content for the tag element <content />. However my code produces a JavaScript alert error which is an error reading the response - Type Error Null value Returned.

    The XML Returned is valid.

    Can anyone help or give advice which is the simplest and effective way of output the data from the PHP Generated XML file?

    Thanks

    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('content');
    		outputText = document.write(responseText);
    		//display the user message
    		
    			
    			myDiv = document.getElementById("myDivElement");
    			myDiv.innerHTML = outputText;
    	}
    PHP Code:
    <?php 
    ini_set
    ('output_buffering'false); 
    ini_set('implicit_flush'true); 


    require_once(
    'error_handler.php');
    require_once(
    'classes/database.class.php'); 

    header('Content-Type: text/xml');

    $country $_GET['country'];
    $table_id 'country';
    $connection = new database();
    $link $connection->database_connection();
    $query "select * from country where location ='$country'";
    $results mysql_query($query) or die(mysql_error());
    if (!
    $results)    
        {
            print 
    'There was a database error when executing';
            print 
    mysql_error();
            exit;
        }    
    //create new xml document
    $doc = new DomDocument();


    // create root node
    $root $doc->createElement('response');
    $root $doc->appendChild($root);

    //process one row at a time
    while($row mysql_fetch_assoc($results)) {


    //add node for each row

    $occ $doc->createElement($table_id);
    $occ $root->appendChild($occ);

    //add a child node for each field
    foreach ($row as $fieldname => $fieldvalue) {

    $child $doc->createElement($fieldname);
    $child $occ->appendChild($child);

    $value $doc->createTextNode($fieldvalue);
    $value $child->appendChild($value);

        } 
    //foreach
    //while

    $xml_string $doc->saveXML();

    echo 
    trim($xml_string);

  2. #2
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    I don't know too much about PHP and I don't know anything about XMLHttpRequest(), but perhaps this is a problem
    Code:
    var id = document.getElementById("country").value;
    					
    //create params string
    					
    var params = "country="+id;
    params is going to literally equal something like "country=[object X]" or "country=[object]", where X is the object type. Maybe you already realize it, but in case you don't the "[object X]" in param is a string, not the actual object.

    EDIT: Oh, I see, I thought you were returning the object, not the object's value. My mistake.
    Last edited by Trinithis; 06-05-2007 at 05:18 PM.

  3. #3
    Join Date
    Jun 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thanks for looking but that isn't the problem i've already looked at that area in detail. Anymore ideas?

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
  •