PHP: Generating an XML Response for Ajax Applications
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;
class xmlResponse
{
// Original PHP code by Chirp Internet: www.chirpinternet.eu
// Please acknowledge use of this code by including this header.
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 = [], $encoded = [])
{
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;
}
}
Copy this code and save it as xmlresponse.php for the following examples to work.
Instructions
The PHP code presented here, using the xmlResponse class:
<?PHP
$retval = "output of PHP script";
$xml = new \Chirp\xmlResponse();
$xml->start();
$xml->command("setstyle", [
"target" => "output",
"property" => "display",
"value" => "block"
]);
$xml->command("setcontent", [
"target" => "samplecode",
"content" => htmlentities($retval),
]);
$xml->end();
exit;
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>
If none of this is making sense, you should first read the article Web Services using XMLHttpRequest and then come back.
Related Articles - Ajax
- JavaScript Making an asynchronous request before following an href
- JavaScript Form Validation using Ajax
- JavaScript Avoiding the Race Condition with Ajax
- JavaScript Using a Promise to make sequential Ajax requests
- JavaScript Making sure form values are unique using Ajax
- JavaScript Using XMLHttpRequest to log JavaScript errors
- JavaScript Recording Outbound Links using Ajax
- JavaScript Making a HEAD request via an Ajax script
- PHP Generating an XML Response for Ajax Applications
- PHP Ajax script for making cURL HEAD requests
- JavaScript Web Services using XMLHttpRequest (Ajax)
Gale 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?
CJ 25 May, 2013
Or.. you can use PHP's
SimpleXMLElement..
geeta 16 November, 2011
Above solution is really easy to understand for form validation using ajax thanks.........
Brian Gottier 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.