Skip to main content

XForms tip: Accepting XForms data in PHP

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.

Summary:  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.

View more content in this series

Date:  26 Sep 2006
Level:  Intermediate
Activity:  2713 views

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

DescriptionNameSizeDownload method
PHP XForms sample codephpdatafiles.zip3KB HTTP

Information about download methods


Resources

Learn

Get products and technologies

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

Discuss

About the author

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.

Comments (Undergoing maintenance)



Trademarks  |  My developerWorks terms and conditions

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

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
author1-email=ibmquestions@nicholaschase.com
author1-email-cc=

My developerWorks community

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Rate a product. Write a review.

Special offers