Using XForms and PHP


A few tips on using XForms and PHP together.

Returning XML data

To either serve a page (with XForms in it) or to generate data used as instance data for XForms, the page needs to be served as XML, for example application/xhtml+xml. To do that, you need to use the header() PHP function:

  header("Content-Type: application/xhtml+xml; charset=UTF-8");

But remember this (quote from PHP documentation): Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

Parsing Submitted Data

Depending on the submission type, you might get different data formats on the server side. To really use the power of XForms (/XML) you would probably submit XML. There is some help in getting to the raw XML on the PHP Documentation site.

How to actually parse and use the XML data depends heavily on what you want to achieve and on the PHP version. To get a DOM Document on PHP 4 I usually do something like this:

$dom = domxml_open_mem($data);
$root = $dom->document_element();
$newnode = $dom->create_element("boo");