HTML Logo by World Wide Web Consortium (www.w3.org). Click to learn more about our commitment to accessibility and standards.

Moving forward with Composr

ocPortal has been relaunched as Composr CMS, which is now in beta. ocPortal 9 will be superseded by Composr 10.

Head over to compo.sr for our new site, and to our migration roadmap. Existing ocPortal member accounts have been mirrored.


What's the correct way to use AJAX to send HTML

Login / Search

 [ Join | More ]
 Add topic 
Posted
Rating:
#72633 (In Topic #15236)
Avatar

Community saint

I'm experimenting with AJAX at the moment with the aim of being able to sending HTML to the browser. I've searched the code for examples but could not find anything 'obvious' for passing HTML.

Here is my php code that returns the tooltip for admin. Am I supposed to escape the HTML (i.e. the output from evaluate_echo() ), or manually wrap it in CDATA, or is there a specific function I am supposed to call?

Code (php)

        require_code('ocf_members');
        require_code('ocf_members2');

        header('Content-Type: text/xml');
        echo '<?xml version="1.0" encoding="'.get_charset().'"?'.'>';
        echo '
<request><result>';
        ocf_show_member_box(2)->evaluate_echo();
        echo '
</result></request>';

On the browser side, I have the following code which works fine for plain text, but will no doubt need tweaking depending on what is done on the php side.

Note that this is a quick-and-dirty test, hence the use of a simple document.write.

Code

    <script type="text/javascript">
    var result = load_XML_doc("{$BASE_URL_NOHTTP#}.../LG_google_test.php",null);
    document.write(result.responseXML.getElementsByTagName('result')[0].text);
    </script>

And for completeness, here is the php output.

Code

<?xml version="1.0" encoding="ISO-8859-1"?><request><result>
   <img class="ocf_member_box_avatar" src=".../Admin.png" alt="Avatar" title="Avatar" />


<div class="ocf_member_box_avatar">
   <table class="tooltip_fields variable_table" summary="Table with headers in left column and values or settings in right column">
      <tbody>
         <tr><th class="de_th">Username:</th><td><a href=".../pg/members/view/admin">admin</a></td></tr>
         <tr><th class="de_th">Forum posts:</th><td>1</td></tr>

         
         <tr><th class="de_th">Joined:</th><td>03 March 2011, 3:41 PM</td></tr>
         
            
               <tr><th class="de_th">IP address:</th><td>000.000.000.000</td></tr>
            
            
            
            
         
         <tr>
            <th class="de_th">Usergroups:</th>
            <td>Staff, Administrators</td>
         </tr>

         <tr><th class="de_th">Online now:</th><td>Yes</td></tr>
         
      </tbody>
   </table>
</div>
</result></request>

Do you have a Samsung Galaxy S / Galaxy S II ? If so, why not check out my ScreenFree FM Radio .
Back to the top
 
Posted
Rating:
#72717
Avatar

Community saint

No takers? Ok, I'll go the CDATA route for now.

New PHP code:

Code (php)

        require_code('ocf_members');
        require_code('ocf_members2');

        header('Content-Type: text/xml');
        echo '<?xml version="1.0" encoding="'.get_charset().'"?'.'>';
        echo '
<request><result>';
        echo '
<![CDATA[';
        ocf_show_member_box(2)->evaluate_echo();
        echo '
]]>';
        echo '
</result></request>';

New javascript code:

Code

    <script type="text/javascript">
    var result = load_XML_doc("{$BASE_URL_NOHTTP#}.../LG_google_test.php",null);
    document.write(result.responseXML.documentElement.getElementsByTagName('result')[0].firstChild.nodeValue);
    </script>

Do you have a Samsung Galaxy S / Galaxy S II ? If so, why not check out my ScreenFree FM Radio .
Back to the top
 
1 guests and 0 members have just viewed this: None
Control functions:

Quick reply   Contract

Your name:
Your message: