XForms – The new Web standard for forms
Definitely XForms is the next generation of Web-based data processing. XForms replaces traditional HTML forms with an XML data model and presentation elements.
In some ways, 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. Here you will see how to create a PHP script that can receive and work with XML data submitted by an XForms form.
Example: Simple XForms search form
- <h:html xmlns:h=“http://www.w3.org/1999/xhtml”
- xmlns=“http://www.w3.org/2002/xforms”>
- <h:head>
- <h:title>Search</h:title>
- <model>
- <submission action=“http://example.com/search”
- method=“post” id=“ss”/>
- </model>
- </h:head>
- <h:body>
- <h:p>
- <input ref=“find”><label>Find</label></input>
- <submit submission=“submit”><label>Go</label></submit>
- </h:p>
- </h:body>
- </h:html>
The above form displays a text input box (named find), and a submit button. When the submit button is clicked, the form will be sent to the page referred to by action.
Here’s where it starts to look different from your web application’s point of view. In a normal HTML form, the data would be sent as application/x-www-form-urlencoded, in the XForms world however, this information is sent as XML formatted data.
If you’re choosing to work with XForms then you probably want that data as XML, in that case, look in $HTTP_RAW_POST_DATA where you’ll find the XML document generated by the browser which you can pass into your favorite XSLT engine or document parser.
If you’re not interested in formatting and just want your data to be loaded into the traditional $_POST variable, you can instruct the client browser to send it as application/x-www-form-urlencoded by changing the method attribute to urlencoded-post.
Example: Using an XForm to populate $_POST
- <h:html xmlns:h=“http://www.w3.org/1999/xhtml”
- xmlns=“http://www.w3.org/2002/xforms”>
- <h:head>
- <h:title>Search</h:title>
- <model>
- <submission action=“http://example.com/search”
- method=“urlencoded-post” id=“ss”/>
- </model>
- </h:head>
- <h:body>
- <h:p>
- <input ref=“find”><label>Find</label></input>
- <submit submission=“submit”><label>Go</label></submit>
- </h:p>
- </h:body>
- </h:html>
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.
Example: 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.
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
Now working with the data
Example: An instance document
- <?xml version=“1.0″ encoding=“UTF-8″?>
- <bookstore>
- <book>
- <book_id>BOOK001</book_id>
- <book_name>Introduction to Electrodynamics</book_name>
- <isbn_no>0000979001</isbn_no>
- <book_price>95.00<book_price>
- </book>
- <book>
- <book_id>BOOK002</book_id>
- <book_name>Understanding of Steel Construction</book_name>
- <isbn_no>0000979002</isbn_no>
- <book_price>115.50<book_price>
- </book>
- <book>
- <book_id>BOOK003</book_id>
- <book_name>This is Guide to Networking</book_name>
- <isbn_no>0000979003</isbn_no>
- <book_price>210.00<book_price>
- </book>
- <book>
- <book_id>BOOK004</book_id>
- <book_name>This is Transfer of Heat and Mass</book_name>
- <isbn_no>0000979004</isbn_no>
- <book_price>260.00<book_price>
- </book>
- </bookstore>
Example: Form to manage 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>This is XForms in PHP Example </title>
- <xforms:model id=“model_bookstore”>
- <xforms:instance id=“instance_model_bookstore” src=“bookstore.xml”/>
- <xforms:submission id=“submit_model_bookstore”
- action=“http://localhost/php/xforms/xforms.php”
- method=“post”/>
- </xforms:model>
- </head>
- <body>
- <xforms:submit submission=“submit_model_bookstore”>
- <xforms:label>Submit</xforms:label>
- </xforms:submit>
- </body>
- </html>
Example: PHP script to work with 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;
- echo “There are ”.$numBooks.“ books”;
- ?>
The $HTTP_RAW_POST_DATA variable is not set by default in many PHP installations; it requires specific configuration changes. You can populate it manually by using the file_get_contents() function to read the data from the input stream.
Next you can 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.
Follow @phpzag

Actually XForm is defined for a while and the biggest problem is no one seems to adopt it (correct me if I’m wrong). Only see one reference on
IBM product few years ago.
Uh, XFORMS has been around for years, almost ten years now. It isn’t well supported, or possibly not supported at all, across the browser vendors.
If it hasn’t been adopted yet, then it’s not going to in the practical future. Sorry.
Yes … the design is clearly needed to be changed
What would be brighter , nebudu (
Greeting from across the sea. precise article I shall return for more.
salutations from over the sea. Great post I will return for more.
Superb site you have here but I was curious if you knew of any message boards that cover the same topics talked about in this article? I’d really like to be a part of community where I can get advice from other knowledgeable people that share the same interest. If you have any suggestions, please let me know. Cheers!