2KB项目,专业的源码交易网站 帮助 收藏 每日签到

XForms – 新的 Web 表单标准

  • 时间:2019-01-23 18:43 编辑:2KB 来源:2KB.COM 阅读:369
  • 扫一扫,手机访问
  • 分享
摘要: 英文原文:XFo
英文原文:XForms – The new Web standard for forms

XForms 肯定是下一代的基于 Web 的数据处理技术规范,XForms 可替换传统的 HTML 表单,使用 XML 数据模型和呈现元素。

在某些情况下,XForms 就等同于 HTML 表单,通过合适的编码服务器端接受的数据跟普通的表单没有区别。但是 XForms 表单比 HTML 表单要强很多,包括可直接用 XML 格式进行数据提交。这里我们将创建一个 PHP 脚本来接收通过XForms 提交过来的 XML数据。

示例: 使用 XForms 的搜索表单
<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>


上述表单显示一个文本输入框和一个提交按钮,当点击提交按钮时将会提交表单到 action。

这里跟普通的 web 应用有点不同,在一个正常的 HTML 表单中,数据使用 application/x-www-form-urlencoded 编码进行发送,而 XForms 的世界里,数据默认使用 XML 格式。

如果你选择了 XForms 那么你在服务器上可通过 $HTTP_RAW_POST_DATA 来获取表单提交上来的 XML 文档数据。

如果你对格式不感兴趣,可通过 $_POST变量获取提交的数据。你也可以要求浏览器使用 application/x-www-form-urlencoded 这个传统的编码来提交数据,只需要修改 method 属性为 urlencoded-post 即可.

示例: 使用 XForm 填充 $_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>


接下来我们使用 PHP 来接收数据,XForms 表单数据使用 POST 方法提交,因此我们需要在 PHP 中访问 $HTTP_RAW_POST_DATA 变量:

示例:基本的 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;
?>

很多默认的 PHP 安装是不会设置 $HTTP_RAW_POST_DATA 变量的,它必须指定配置才能启用。幸运的是你还可以使用 file_get_contents() 函数来从输入数据中读取。

一旦你将数据转成字符串就可直接作为纯文本输出到浏览器,只需设置 content-type 为 text/plain 即可。这样当你提交表单时就可以看到使用 XML 格式的表单数据。

很棒,但实际中怎么处理呢?PHP可以让你很方便的直接通过XML格式字符串创建一个 DOC 文档对象。

接下来我们来处理数据。
示例:一个示例XML文档
<?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>


示例:管理数据的表单
<?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.

本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接。 2KB翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。


2KB项目(www.2kb.com,源码交易平台),提供担保交易、源码交易、虚拟商品、在家创业、在线创业、任务交易、网站设计、软件设计、网络兼职、站长交易、域名交易、链接买卖、网站交易、广告买卖、站长培训、建站美工等服务

  • 全部评论(0)
资讯详情页最新发布上方横幅
最新发布的资讯信息
【计算机/互联网|】Nginx出现502错误(2020-01-20 21:02)
【计算机/互联网|】网站运营全智能软手V0.1版发布(2020-01-20 12:16)
【计算机/互联网|】淘宝这是怎么了?(2020-01-19 19:15)
【行业动态|】谷歌关闭小米智能摄像头,因为窃听器显示了陌生人家中的照片(2020-01-15 09:42)
【行业动态|】据报道谷歌新闻终止了数字杂志,退还主动订阅(2020-01-15 09:39)
【行业动态|】康佳将OLED电视带到美国与LG和索尼竞争(2020-01-15 09:38)
【行业动态|】2020年最佳AV接收机(2020-01-15 09:35)
【行业动态|】2020年最佳流媒体设备:Roku,Apple TV,Firebar,Chromecast等(2020-01-15 09:31)
【行业动态|】CES 2020预览:更多的流媒体服务和订阅即将到来(2020-01-08 21:41)
【行业动态|】从埃隆·马斯克到杰夫·贝佐斯,这30位人物定义了2010年代(2020-01-01 15:14)
联系我们

Q Q: 7090832

电话:400-0011-990

邮箱:7090832@qq.com

时间:9:00-23:00

联系客服
商家入住 服务咨询 投拆建议 联系客服
0577-67068160
手机版

扫一扫进手机版
返回顶部