©
Dokumen ini menggunakan Manual laman web PHP Cina Lepaskan
PHP 一个很有用的特点体现在它处理 PHP 表单的方式。需要理解的非常重要的原理,是表单的任何元素都在 PHP 脚本中自动生效。请参阅本手册中“PHP 的外部变量”以获取关于在 PHP 中使用表单的详细信息及范例。以下是 HTML 表单的范例:
Example #1 一个简单的 HTML 表单
<form action="action.php" method="post"> <p>姓名: <input type="text" name="name" /></p> <p>年龄: <input type="text" name="age" /></p> <p><input type="submit" /></p> </form>
该表单中并没有什么特殊的地方,其中没有使用任何特殊的标识符。当用户填写了该表单并点击了提交按钮,页面 action.php 将被调用。在该文件中,可以加入如下内容:
Example #2 打印来自表单的数据
你好,<?php echo htmlspecialchars ( $_POST [ 'name' ]); ?>。
你 <?php echo (int) $_POST [ 'age' ]; ?> 岁了。
该脚本的输出可能是:
你好,Joe。你 22 岁了。
除了 htmlspecialchars() 和 (int) 部分,这段程序做什么用显而易见。 htmlspecialchars() 使得 HTML 之中的特殊字符被正确的编码,从而不会被使用者在页面注入 HTML 标签或者 Javascript 代码。例如 age 字段,我们明确知道他是一个数值,因此我们将它转换为一个 整形值(integer) 来自动的消除任何不必要的字符。也可以使用 PHP 的 filter 扩展来自动完成该工作。PHP 将自动设置 $_POST['name'] 和 $_POST['age'] 变量。在这之前我们使用了超全局变量 $_SERVER ,现在我们引入了超全局变量 $_POST,它包含了所有的 POST 数据。请注意我们的表单提交数据的方法(method)。如果使用了 GET 方法,那么表单中的信息将被储存到超全局变量 $_GET 中。如果并不关心请求数据的来源,也可以用超全局变量 $_REQUEST,它包含了所有 GET、POST、COOKIE 和 FILE 的数据。
也可以在 PHP 中处理 XForms 的输入,尽管用户可能更喜欢使用长久以来支持良好的 HTML 表单。XForms 目前还不适合初学者使用,但是用户可能对它感兴趣。手册中在“特点”一章有一节对如何处理从 XForum 接收到的数据进行了简短的介绍。
[#1] mail at pheunix dot in [2015-04-18 10:00:36]
Thanks guys for sharing information about 12 Free And Open Source PHP Forum Scripts.
Software company in rajasthan
www.pheunix.in
[#2] mail at pheunix dot in [2015-04-17 11:56:34]
I have loved having for my website and also my amazing provider.! So much better and faster.
Software company in rajasthan
www.pheunix.in
[#3] wojciech dot fornal at gmail dot com [2015-01-04 11:14:04]
@sethg at ropine dot com
You're partially right. For many people, the difference between POST/GET is about whether data is sent as a URL query (GET) or as a HTTP request payload together with headers (POST) and in most cases it is used so with regards to that.
In case of forms the difference between GET and POST has more to do with convenience and the fact that both methods fit to certain use cases and not with the fact whether a some resource is created/changed on the server or not (eg. login forms use POST method mainly to not expose sensitive data in URL etc.). It all depends on the back-end implementation what really happens after GET or POST request is received.
GET is good if you want the request to be cacheable and/or bookmarkable. In most HTML form cases though, POST seems always better, especially when we deal with long data (eg. forum post).
To be strict about HTTP verbs, POST verb usually means creation of new resource while to update an existing resource, the PUT method is used (not applicable in case of HTML forms except some additional hidden "method" form fields).
Those who are not familiar with HTTP verbs shall dive into HTTP specs (RFC 2616, section "9 Method Definitions") and read a bit about REST.
[#4] mail at pheunix dot in [2014-10-15 07:10:30]
It's true, comments do not take up PROCESSING time, but they do take some PARSING time in case you are not using a compile cache of some kind.
[#5] jerry4u at foxmail dot com [2014-05-27 04:23:30]
the difference of method GET and POST is really important.
[#6] behnam jaza faza [2013-06-24 07:22:28]
The note above says:
"Also see the import_request_variables() function. "
But dont:
This function has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0.
[#7] Andy [2012-04-14 00:05:21]
Just a reminder about security: the chosen HTTP request verb (e.g. POST, GET) does not necessarily make the request "secure". Any information that is not transmitted over an encrypted channel (using SSL, i.e. HTTPS) is transmitted in plan text.
For secure transport of sensitive/private information over HTTP consider using SSL as this prevents eve's dropping of the information transmitted over HTTP.
[Edited by googleguy@php.net for clarity]
[#8] Johann Gomes (johanngomes at gmail dot com) [2010-10-11 19:20:57]
Also, don't ever use GET method in a form that capture passwords and other things that are meant to be hidden.
[#9] sethg at ropine dot com [2003-12-01 12:55:25]
According to the HTTP specification, you should use the POST method when you're using the form to change the state of something on the server end. For example, if a page has a form to allow users to add their own comments, like this page here, the form should use POST. If you click "Reload" or "Refresh" on a page that you reached through a POST, it's almost always an error -- you shouldn't be posting the same comment twice -- which is why these pages aren't bookmarked or cached.
You should use the GET method when your form is, well, getting something off the server and not actually changing anything. For example, the form for a search engine should use GET, since searching a Web site should not be changing anything that the client might care about, and bookmarking or caching the results of a search-engine query is just as useful as bookmarking or caching a static HTML page.