XForms in PHP

In this tutorial, you will see how to create a PHP script that can receive and work with XML data submitted by an XForms form.

An instance document  

<?xml version="1.0" encoding="UTF-8"?>
<book_name>Introduction to Electrodynamics</book_name>
<book_name>Understanding of Steel Construction</book_name>
<book_name>Guide to Networking</book_name>
<book_name>Transfer  of Heat and Mass</book_name>

Form to manage data

<?xml version="1.0"?>
<html xmlns="http://www.w3.org/1999/xhtml"
<title>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"
<xforms:submit submission="submit_model_bookstore">

PHP script to work with data

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

Previous: Cookies
Next: Php error handling Installation and runtime configuration

PHP: Tips of the Day

PHP - how to create a newline character?

Only double quoted strings interpret the escape sequences \r and \n as '0x0D' and '0x0A' respectively, so you want:


Single quoted strings, on the other hand, only know the escape sequences \\ and \'.

So unless you concatenate the single quoted string with a line break generated elsewhere (e. g., using double quoted string "\r\n" or using chr function chr(0x0D).chr(0x0A)), the only other way to have a line break within a single quoted string is to literally type it with your editor:

$s = 'some text before the line break
some text after';

Make sure to check your editor for its line break settings if you require some specific character sequence (\r\n for example).

Ref : https://bit.ly/3hcyege