Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post

    Issue with AJAX and XML

    What am I missing here? The "alert(x.length)" displays "0", below is the JavaScript:

    PHP Code:
    //Check for any errors every 5 seconds
    function error_checking() {
        var 
    http getHTTPObject();
        
    http.onreadystatechange=function() {
            var 
    txt "<table border='1'><tr><th>Message</th><th>ID</th><td>Date</th><th>Message or Error</th></tr>";
            if (
    http.readyState==&& http.status==200) {
                var 
    http.responseXML.documentElement.getElementsByTagName("messages");
                
    alert(x.length);
                for(var 
    i=0;i<x.length;i++) {
                    
                    
    //Enter message details into table
                    
    txt=txt+"<tr>";
                    var 
    xx x[i].getElementsByTagName("msg");
                    try {
                        
    txt=txt+"<td>"+xx[0].firstChilde.nodeValue+"</td>";    
                    } catch (
    er) {
                        
    txt=txt+"<td&nbsp;</td>";    
                    }
                    
                    
    //Enter ID details into table
                    
    var xx x[i].getElementsByTagName("id");
                    try {
                        
    txt=txt+"<td>"+xx[0].firstChilde.nodeValue+"</td>";    
                    } catch (
    er) {
                        
    txt=txt+"<td&nbsp;</td>";    
                    }
                    
                    
    //Enter date details into table
                    
    var xx x[i].getElementsByTagName("date");
                    try {
                        
    txt=txt+"<td>"+xx[0].firstChilde.nodeValue+"</td>";    
                    } catch (
    er) {
                        
    txt=txt+"<td&nbsp;</td>";    
                    }
                    
                    
    //Enter message or error details into table
                    
    var xx x[i].getElementsByTagName("messerror");
                    try {
                        
    txt=txt+"<td>"+xx[0].firstChilde.nodeValue+"</td>";    
                    } catch (
    er) {
                        
    txt=txt+"<td&nbsp;</td>";    
                    }
                
    txt=txt "</txt>";
                }
            
    txt=txt+"</table>";
            
    doc("test").innerHTML txt;
            }
        }
        
    http.open("GET","php/check_errors.php",true);
        
    http.send();

    Here is the XML code:
    PHP Code:
    <messages>
    <
    msg>
    <
    id>32</id>
    <
    date>14/11/2013 20:37</date>
    <
    message>Successfully logged in!</message>
    <
    messerror>message</messerror>
    </
    msg>
    <
    msg>
    <
    id>33</id>
    <
    date>15/11/2013 00:52</date>
    <
    message>Successfully logged in!</message>
    <
    messerror>message</messerror>
    </
    msg>
    <
    msg>
    <
    id>34</id>
    <
    date>15/11/2013 02:42</date>
    <
    message>Successfully logged in!</message>
    <
    messerror>message</messerror>
    </
    msg>
    </
    messages

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 Posts
    var x = http.responseXML.documentElement.getElementsByTagName("messages");

    should be ...

    var x = http.responseXML.documentElement.getElementsByTagName("msg");


    messages is the root and you already
    have that in x

    firstChilde
    shoujd be ...
    firstChild

    txt=txt+"<td&nbsp;</td>";
    should be ...
    txt=txt+"<td>&nbsp;</td>";

    http://codingforums.daveyerwin.com/martynball/
    Last edited by DaveyErwin; 11-15-2013 at 12:40 PM.

  • Users who have thanked DaveyErwin for this post:

    martynball (11-15-2013)

  • #3
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    Quote Originally Posted by DaveyErwin View Post
    var x = http.responseXML.documentElement.getElementsByTagName("messages");

    should be ...

    var x = http.responseXML.documentElement.getElementsByTagName("msg");


    messages is the root and you already
    have that in x

    firstChilde
    shoujd be ...
    firstChild

    txt=txt+"<td&nbsp;</td>";
    should be ...
    txt=txt+"<td>&nbsp;</td>";

    http://codingforums.daveyerwin.com/martynball/
    Thanks, why do I always miss my damn spelling mistakes! Lol


  •  

    Posting Permissions

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