Getting HTML block via ResponseXML? (Ajax)

Discussion in 'JavaScript' started by tailrunner, Jun 14, 2010.

  1. #1
    Hi!
    I am currently developing a kind of CMS framework administration system, in which I would need to fetch HTML code through Ajax from a database along with other data and put in input fields. Fetching ordinary strings and values through the PHP generated XML document works perfect, but as soon as I fetch html content, the whole thing bugs out and gives me NULL-values. The $html gets into the mysql database through a WYSIWYG editor. As longs as the row contains no tags, the XML fetching in javascript works flawlessly.

    This is my generated XML document: (Short-tags on the server is turned off)

    
    <?php
    header("Content-Type: application/xml");
    ?>
    <?xml version="1.0" encoding="ANSI"?>
    
    <?php
    
    $id = $_GET['id'];
    
    $result = mysql_query("SELECT * FROM page WHERE ID ='$id'") or die(mysql_error());
    while($row = mysql_fetch_array($result)){
    	$title = mysql_escape_string($row['title']);
    	$html = mysql_escape_string($row['html']);
    	echo "
    	<DATA>
    		<TITLE>$title</TITLE>
    		<VISIBLE>$row[visible]</VISIBLE>
    		<TEMPLATE>$row[templateID]</TEMPLATE>
    		<HTML>$html</HTML>
    	</DATA>";
    	}
    
    
    PHP:
    My javascript fetching code:
    
    		onSuccess: function(transport){
    			var root = transport.responseXML;
    			alert(transport.responseText);
    			var pageTitle, pageHTML, pageVisible, pageTemplate;
    			try{pageTitle = root.getElementsByTagName("TITLE")[0].firstChild.data}catch(e){debug(e)};
    			try{pageHTML  = root.getElementsByTagName("HTML")[0].firstChild.data}catch(e){debug(e)};
    			if(!pageHTML) pageHTML = " ";
    
    
    PHP:

    In my alert, I get the full XML page. however, in the occurence of HTML in the $html, and not when $html is just plain text, the try{} throws these errors:

    
    TypeError: root.getElementsByTagName("TEMPLATE")[0] is undefined
    
    TypeError: root.getElementsByTagName("VISIBLE")[0] is undefined
    
    TypeError: root.getElementsByTagName("HTML")[0] is undefined
    
    TypeError: root.getElementsByTagName("TITLE")[0] is undefined
    
    Code (markup):
    Not just for the HTML block, but for everything!!
    My guess is that html in an XML document screws with the XML DOM.


    Is it possible to fetch HTML through XML? I haven't tried with JSON yet, is that possible? Heard it's quicker, but that doesn't really matter.

    Thanks in advance!!
     
    tailrunner, Jun 14, 2010 IP
  2. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #2
    stephan2307, Jun 14, 2010 IP
  3. tailrunner

    tailrunner Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Doh, thank you very much! Exactly what I've been looking for!
    I've even been to W3Schools before, but searching after a new way to fetch the results, not so send them.
     
    Last edited: Jun 14, 2010
    tailrunner, Jun 14, 2010 IP
  4. GFX_Lover

    GFX_Lover Peon

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You can always use RegEx to extract data from HTML :)

    function fetchData(tag, data){
    reg = new RegExp("<" + tag + ">([^<>]+)<\/" + tag + ">", "i");
    return reg.exec(data)[1];
    }

    alert("title", transport.responseText);
     
    GFX_Lover, Jun 29, 2010 IP