XForms tip: Accepting XForms data in PHP

In some ways, an XForms form is just like an HTML form; with the proper encoding, the server-side script receiving the data won't even know the difference. But the strength of XForms forms is in many ways the fact that the data can be submitted directly as XML. Of course, this capability doesn't do you any good unless the script is prepared to receive the data. In this tip, you will see how to create a PHP script that can receive and work with XML data submitted by an XForms form.

Nicholas Chase (ibmquestions@nicholaschase.com), Freelance Writer

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.



26 September 2006

Also available in Russian

Before you begin: Notes about this tip

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.


The instance document

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
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;
?>

The $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
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
the final result

Summary

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 $HTTP_RAW_POST_DATA variable.


Download

DescriptionNameSize
PHP XForms sample codephpdatafiles.zip3KB

Resources

Learn

Get products and technologies

  • Get MozzIE, an open-source control that allows you to render XForms in IE.

Discuss

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name



The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


All information submitted is secure.

Dig deeper into XML on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=XML
ArticleID=160527
ArticleTitle=XForms tip: Accepting XForms data in PHP
publish-date=09262006