skip to content

PHP: Generating an XML Response for Ajax Applications

 Tweet0 Shares0 Tweets

This article shows how to easily generate an XML response using PHP. The purpose in this case is to send the XML in response to a JavaScript Ajax request. For more on XMLHttpRequest and Ajax see the JavaScript section of this site where you can find a number of related articles.

Introducing the xmlResponse class

The PHP class presented here may evolve over time but will always be quite simple as it performs a very simple task - converting PHP variables into a valid XML format suitable for use with the AjaxRequest JavaScript class.

For now there are just three public functions - start to output the XML header, command to output a command element and end to close the response and exit.

<?PHP   namespace Chirp; // Original PHP code by Chirp Internet: // Please acknowledge use of this code by including this header. class xmlResponse {   function start()   {     header("Content-Type: text/xml");     echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' "\n";     echo "<response>\n";   }   function command($method$params=array(), $encoded=array())   {     echo "  <command method=\"$method\">\n";     if($params) {       foreach($params as $key => $val) {         echo "    <$key>" htmlspecialchars($val) . "</$key>\n";       }     }     if($encoded) {       foreach($encoded as $key => $val) {         echo "    <$key><![CDATA[$val]]></$key>\n";       }     }     echo "  </command>\n";   }   function end()   {     echo "</response>\n";     exit;   } } ?>

expand code box

Copy this code and save it as xmlresponse.php for the following examples to work.


The PHP code presented here, using the xmlResponse class:

<?PHP $retval = "output of PHP script"; $xml = new xmlResponse(); $xml->start(); $xml->command("setstyle", array("target" => "output", "property" => "display", "value" => "block") ); $xml->command("setcontent", array("target" => "samplecode"), array("content" => htmlentities($retval)) ); $xml->end(); ?>

will generate the following XML output (preceded by an XML Content-type header):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <response> <command method="setstyle"> <target>output</target> <property>display</property> <value>block</value> </command> <command method="setcontent"> <target>samplecode</target> <content><![CDATA[output of PHP script]]></content> </command> </response>

The name/value pairs in the first array will be added directly to the command, while values in the second array will appear within a CDATA section. If none of this is making sense, you should first read the article Web Services using XMLHttpRequest and then come back.


Send a message to The Art of Web:

used only for us to reply, and to display your gravatar.


<- copy the digits from the image into this box

press <Esc> or click outside this box to close

User Comments

Post your comment or question

13 August, 2015

Hi! Thanks for sharing your class.xmlresponse.php. How do I pass empty string to $retval so I can clear up previous $retval that was displayed?

25 May, 2013

Or.. you can use PHP's

16 November, 2011

Above solution is really easy to understand for form validation using ajax thanks.........

12 August, 2007

In this tutorial, it would be great if you could share your validate.php script. I would like to see how the validation is actually happening, and how the alert messages such as "you are too young" are being generated.

I've shared a part of the script now and you can find it in the Form Validation using Ajax article. What's missing is just basic programming.