This tip is specific to a particular task. For background information on XForms, see the three-part series Introduction to XForms.
The XForms samples described in this tip have been tested and work with Firefox 1.5 (with the XForms extension installed) and Microsoft® Internet Explorer 6 with the Formsplayer control installed. The download contains the XHTML file for Firefox and the HTML file for IE.
Start with an instance document. In order to have something to work with, create an instance that has a bit of depth to it (see Listing 1).
Listing 1. The instance document
<?xml version="1.0" encoding="UTF-8"?> <inventory> <book> <author>Nick Chase</author> <title>XML Primer Plus</title> <publisher>Sams</publisher> </book> <book> <author>Earth</author> <title>David Brin</title> <publisher>Spectra</publisher> </book> <movie> <director>Steven Spielberg</director> <title>Raiders of the Lost Ark</title> <actor>Harrison Ford</actor> <actor>Karen Allen</actor> </movie> <movie> <director>Rob Reiner</director> <title>The American President</title> <actor>Michael Douglas</actor> <actor>Annette Bening</actor> <actor>Martin Sheen</actor> </movie> </inventory>
The form to manage this data is fairly straightforward (see Listing 2).
Listing 2. Managing the data
<?xml version="1.0"?> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms"> <head> <title>Instance Data-To-XHTML/XForms Example</title> <xforms:model id="model_inventory"> <xforms:instance id="instance_model_inventory" src="inventory.xml"/> <xforms:submission id="submit_model_inventory" action="http://localhost/xforms.php" method="post"/> </xforms:model> </head> <body> <xforms:submit submission="submit_model_inventory"> <xforms:label>Submit</xforms:label> </xforms:submit> </body> </html>
The form just submits the instance document as-is, as you can see in Figure 1.
Figure 1. The form
Now let's deal with the actual PHP. The XForms form data gets submitted to the server as the contents of the POST request, so in order to access it using PHP, you'll need to access the content of the
$HTTP_RAW_POST_DATA variable (see Listing 3).
Listing 3. The basic PHP
<?php if (!isset($HTTP_RAW_POST_DATA)) $HTTP_RAW_POST_DATA = file_get_contents("php://input"); header("Content-type: text/plain"); echo $HTTP_RAW_POST_DATA; ?>
$HTTP_RAW_POST_DATA variable is not set by default in many PHP installations; it requires specific configuration changes. Fortunately, you can populate it manually by using the
file_get_contents() function to read the data from the input stream.
Once you have the string representing the XML, you can output it to the browser as plain text by setting the Content-type for the response to text/plain and echoing the contents of the variable. You should see the instance document as it existed when the form was submitted, as seen in Figure 2.
Figure 2. The instance document
That's great, but what about actually working with the data? Fortunately, PHP enables you to easily create a DOM Document from a string of XML formatted text (see Listing 4).
Listing 4. Working with the data
<?php if (!isset($HTTP_RAW_POST_DATA)) $HTTP_RAW_POST_DATA = file_get_contents("php://input"); $doc = new DOMDocument(); q $doc->loadXML($HTTP_RAW_POST_DATA); $allBooks = $doc->getElementsByTagName('book'); $numBooks = $allBooks->length; $allMovies = $doc->getElementsByTagName('movie'); $numMovies = $allMovies->length; echo "There are ".$numBooks." books and ".$numMovies." movies."; ?>
In this case, you create a new DOM Document, and then use the
loadXML() function to load in the data. From there, you can manipulate the Document in any way, just as though you had loaded the data from a file or other source. In this case, the results should look like Figure 3.
Figure 3. The final result
XForms submits XML data as the content of an HTTP POST request, so
to use it in your applications, simply read the contents of the
|PHP XForms sample code||phpdatafiles.zip||3KB||HTTP|
This series is a good guide for getting started using XForms.
Learning PHP", a three-part tutorial series, will get you started understanding PHP.
XML area on developerWorks, get the resources
you need to advance your skills in XForms and other XML technologies.
Open Source area on developerWorks, get the resources
you need to advance your skills in PHP.
technology bookstore for books on these and other technical topics.
Get products and technologies
Get MozzIE, an open-source control that allows you to render XForms in IE.
Nicholas Chase has been involved in Web-site development for companies such as Lucent Technologies, Sun Microsystems, Oracle, and the Tampa Bay Buccaneers. He has been a high school physics teacher, a low-level radioactive waste facility manager, an online science fiction magazine editor, a multimedia engineer, an Oracle instructor, and the chief technology officer of an interactive communications company. He is the author of several books, including XML Primer Plus.