PHP 8.0.26 Released!

处理 XForms

» XForms 定义了传统 web 表单的变种,可以用于更广泛的平台和浏览器,甚至非传统的媒体(例如 PDF 文档)。

XFroms 的第一个关键区别是表单怎样发送到客户端。» XForms for HTML Authors 包含如何创建 XForms 的详细说明。处于本教程的目的,将只看简单的示例。

示例 #1 简单的 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="s"/>
 </model>
</h:head>
<h:body>
 <h:p>
  <input ref="q"><label>Find</label></input>
  <submit submission="s"><label>Go</label></submit>
 </h:p>
</h:body>
</h:html>

上面的表单显示一个文本输入框(名为 q)和一个提交按钮。当点击提交按钮,表单将被发送到 action 所指示的页面。

从 web 应用程序的角度来看,这就是看起来不同的地方。在普通的 HTML 表单中,数据将作为 application/x-www-form-urlencoded 发送,在 XForms 的世界中,该信息是以 XML 格式数据发送的。

如果选择使用 XForms,这种情况下,肯定期望数据为 XML,查看 $HTTP_RAW_POST_DATA 将会找到包含由浏览器生成的 XML 文档,可以将其传递给喜欢的 XSLT 引擎或者文档解析器。

如果对格式不感兴趣,只想让数据加载到传统的 $_POST 变量中,只要将 method 属性修改为 urlencoded-post 就可以指示客户端浏览器将其作为 application/x-www-form-urlencoded 发送。

示例 #2 使用 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="s"/>
 </model>
</h:head>
<h:body>
 <h:p>
  <input ref="q"><label>Find</label></input>
  <submit submission="s"><label>Go</label></submit>
 </h:p>
</h:body>
</h:html>

注意: 在编写本文档时,许多浏览器还不支持 XForms。如果上述例子失败,请检查自己的浏览器版本。

add a note

User Contributed Notes 4 notes

up
14
contact at jimmajammalulu dot com
2 years ago
According to MDN XForms has long been obsolete.
up
16
lphuberdeau at phpquebec dot org
18 years ago
Since HTTP_RAW_POST_DATA requires a configuration to be generated and is not enabled as a default value, you will probably have to use the PHP STDIN stream to get the raw data. It's probably better to use this method as the raw data will not be generated every time, even when not needed.

<?php
$fp
= fopen( "php://stdin", "r" );
$data = '';
while( !
feof( $fp ) )
   
$data .= fgets( $fp );
fclose( $fp );
?>
up
9
OrionI
17 years ago
FireFox has an XForms plugin that works with the latest nightly builds. Check out http://www.mozilla.org/projects/xforms/ for more info. For IE support, there's an ActiveX control from Novell (http://developer.novell.com/xforms/) and one from x-port.net (http://www.formsplayer.com/).

There's also a JavaScript-based one coming out called FormFaces which looks very promising, especially since there are no plugins required and it works in IE, FF, and Opera: http://www.formfaces.com/
up
-8
Darkener Daemon EX
16 years ago
"php://stdin" doesn't exist in my PHP version. I use the following code block instead :
<?php
if (!isset($HTTP_RAW_POST_DATA))
   
$HTTP_RAW_POST_DATA = file_get_contents("php://input");
?>
To Top