NOTE: You can download the PHP installer from http://www.php.net. This example was tested with the default installation. The PHP 4.3.7 installation also includes expat, an XML toolkit written by James Clark that parses XML in C. This is an event based XML parser which we will make use of.
This example shows how to obtain and manipulate submitted XForms instance data within a PHP script. Our PHP script is called EchoPerson.php, and it simply outputs the received data to the browser in a tabular format. (The full PHP code is available here.)
The first thing we need to do in the script is to make sure that there is some data for us to process. Any submitted data will be available to us in the HTTP_RAW_POST_DATA global variable. We check to see if this has a value and if it has, assign the data to a local variable:
...
if (!empty($GLOBALS['HTTP_RAW_POST_DATA'])) {
$req = $HTTP_RAW_POST_DATA;
...
}
...
Once we have some data, we create an XML parser, set up the event handler functions--using xml_set_element_handler() and xml_set_character_data_handler(), and then set any other options we need with xml_parser_set_option:
...
$xmlParser = xml_parser_create();
xml_set_element_handler($xmlParser, "startElement", "endElement");
xml_set_character_data_handler($xmlParser, "characterData");
xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, FALSE);
...
The startElement() and endElement() functions are defined further up in the script and these functions are where we put any code to call when the parser comes across a new element or finishes reading an element. The characterData() function is also defined further up the script, and this deals with what to do with data contained inside the element. Within these functions we also reference two global variables $currentEle and $sHTML. The first is to keep track of the name of the current element that is being processed, and the other will hold the HTML that will eventually be displayed:
...
$currentEle = "";
$sHTML = "";
function startElement($parser, $name, $attrs) {
global $currentEle;
global $sHTML;
$currentEle = $name;
if (!ignoreElement($currentEle)) {
$sHTML .= "$name";
}
}
function endElement($parser, $name) {
global $currentEle;
$currentEle = "";
}
function characterData($parser, $data) {
global $currentEle;
global $sHTML;
if (!ignoreElement($currentEle)) {
$sHTML .= "$data";
}
}
...
There is also one more function defined in the script (ignoreElement()) that is used by a couple of the parser event handlers. As we have elements that don't contain data (person and address) we don't want to display them in our HTML table; the elements we don't want to display have their names contained in an array so that we can compare the current element name (global variable $currentEle) with the values in our array, and decide to ignore the element if we have a match:
...
function ignoreElement($eleName) {
$ignore = false;
$ignoreThese = array(0 => "person",
1 => "address",
2 => ""
);
foreach ($ignoreThese as $value) {
if ($value == $eleName) {
$ignore = true;
break;
}
}
return $ignore;
}
...
Now we actually parse the XML using the conveniently named xml_parse() function. This returns either true or false depending on whether there were any errors or not. If there are any errors we just add them to the HTML string that has been built up:
...
if(!xml_parse($xmlParser, $req, TRUE)) {
$sHTML .= "";
$sHTML .= "An error occurred : ";
$sHTML .= xml_error_string(xml_get_error_code($xmlParser));
$sHTML .= "Line : " . xml_get_current_line_number($xmlParser);
$sHTML .= "Column : " . xml_get_current_column_number($xmlParser);
$sHTML .= "";
}
...
After finishing off the HTML table and freeing up the parser memory (using xml_parser_free()) we output the HTML:
...
print $sHTML;
...
NOTE: Documentation for all PHP functions, including those for the XML parser are available online at http://www.php.net/docs.php.
To try this for yourself:
- Get the
person.htmlandEchoPerson.phpfiles, and place them in the root directory of your website. - Open
person.htmlin notepad, or some other text editor, and change thesubmissionelementactionattribute toEchoPerson.php. - Open up Internet Explorer and go to
http://localhost/person.html, or wherever you have located the file. - Change any data and submit the form.
- The browser page should be replaced with the output form the server.

Recent comments
14 weeks 3 days ago
14 weeks 3 days ago
18 weeks 12 hours ago
19 weeks 3 days ago
19 weeks 3 days ago
19 weeks 4 days ago
19 weeks 5 days ago
19 weeks 6 days ago
19 weeks 6 days ago
19 weeks 6 days ago