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"?>
<bookstore>
<book>
<book_id>BK001</book_id>
<book_name>Introduction to Electrodynamics</book_name>
<isbn_no>0000979001</isbn_no>
<book_price>85.00<book_price>
</book>
<book>
<book_id>BK002</book_id>
<book_name>Understanding of Steel Construction</book_name>
<isbn_no>0000979002</isbn_no>
<book_price>105.50<book_price>
</book>
<book>
<book_id>BK003</book_id>
<book_name>Guide to Networking</book_name>
<isbn_no>0000979003</isbn_no>
<book_price>200.00<book_price>
</book>
<book>
<book_id>BK004</book_id>
<book_name>Transfer of Heat and Mass</book_name>
<isbn_no>0000979004</isbn_no>
<book_price>250.00<book_price>
</book>
</bookstore>
Form to manage data
<?xml version="1.0"?>
<html xmlns="https://www.w3.org/1999/xhtml"
xmlns:ev="https://www.w3.org/2001/xml-events"
xmlns:xforms="https://www.w3.org/2002/xforms">
<head>
<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"
action="https://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>
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 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
How to Sort Multi-dimensional Array by Value?
Try a usort, If you are still on PHP 5.2 or earlier, you'll have to define a sorting function first:
Example:
function sortByOrder($a, $b) { return $a['order'] - $b['order']; } usort($myArray, 'sortByOrder');
Starting in PHP 5.3, you can use an anonymous function:
usort($myArray, function($a, $b) { return $a['order'] - $b['order']; });
And finally with PHP 7 you can use the spaceship operator:
usort($myArray, function($a, $b) { return $a['order'] <=> $b['order']; });
To extend this to multi-dimensional sorting, reference the second/third sorting elements if the first is zero - best explained below. You can also use this for sorting on sub-elements.
usort($myArray, function($a, $b) { $retval = $a['order'] <=> $b['order']; if ($retval == 0) { $retval = $a['suborder'] <=> $b['suborder']; if ($retval == 0) { $retval = $a['details']['subsuborder'] <=> $b['details']['subsuborder']; } } return $retval; });
If you need to retain key associations, use uasort() - see comparison of array sorting functions in the manual
Ref : https://bit.ly/3i77vCC
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework