Results 1 to 6 of 6
  1. #1

    AJAX: Weird problem. (PHP-XML-> JS) XML not parsed properly

    Hello,

    I have created AJAX based Appplication in PHP

    All works fine... Data is "posted" to the php script.
    Data is processed
    XML with proper data is created through the PHP Script (I have checked the same personally. The data is just right)
    However, when I use JS to read the data back on the same HTML form on different fields, improper data is being shown.

    Due to this, the application is not running as expected.

    I again confirm, that the XML generated in PHP is perfect with the tags and data values...

    I would appreciate if someone could throw some light on why this is happening.

    Thank you.

    Best Regards,
    Monil C

  2. #2
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,104
    What is shown, and what do you expect? Post some code please, we can't help you without seeing code.

  3. #3
    Hi,

    The PHP Script creates proper XML

    //Collect Posted variables.

    // Process them

    // Create XML

    $return_value = '<?xml version="1.0" standalone="yes"?><Data><a>123.33</a><b>10</b><c>1234567</c></Data>';

    header('Content-Type: text/xml');
    echo $return_value;
    However, the html page where I am pasting the retrieved values from XML is fetching wrong values.

    function handleHttpResponse() {
    if (http.readyState == 4) {
    if (http.responseText.indexOf('invalid') == -1) {
    // Use the XML DOM to unpack the city and state data
    var xmlDocument = http.responseXML;
    var a = xmlDocument.getElementsByTagName('a').item(0).firstChild.data;
    var b = xmlDocument.getElementsByTagName('b').item(0).firstChild.data;
    var c = xmlDocument.getElementsByTagName('c').item(0).firstChild.data;
    document.getElementById('a').value = a;
    document.getElementById('b').value = b;
    document.getElementById('c').value = c;
    isWorking = false;
    }
    }
    }
    The tel field shows wrong info in fields a, b, c.

    It posts 0, 0.45, 443

    I just dont understadn what is wrong...

    Thanks.

  4. #4
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,104
    when you alert() the data, does it show the correct value?

    Here is the example that I tried with your XML, and it worked fine:
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function test() { alert(xmlDoc.getElementsByTagName('a').item(0).firstChild.data); 
    
    }
    if (document.implementation && document.implementation.createDocument)
    	{
    		xmlDoc = document.implementation.createDocument("", "", null);
    		xmlDoc.onload = test;
    	}
    	else if (window.ActiveXObject)
    	{
    		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    		xmlDoc.onreadystatechange = function () {
    			if (xmlDoc.readyState == 4) test()
    		};
     	}
    	else
    	{
    		alert('Your browser can\'t handle this script');
    		
    	}
    	xmlDoc.load("test.xml");
    
    
    </script>
    </head>
    <body><p>Hello</p></body>
    </html>
    The test.xml:

    Code:
    <?xml version="1.0" standalone="yes"?><Data><a>123.33</a><b>10</b><c>1234567</c></Data>

  5. #5
    Join Date
    May 2006
    Posts
    134
    Its a good idea to check the status code returned from the AJAX request, using the above example:

    if (xmlDoc.readyState == 4) test()


    if (xmlDoc.readyState == 4 && xmlDoc.status == 200) test()

  6. #6
    Thank you for the replies...

Posting Permissions

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