|
|
 |
PHP 一个很有用的特点体现在它处理 PHP 表单的方式。您需要理解的非常重要的原理,是表单的任何元素都在您的 PHP 脚本中自动生效。请参阅本手册“PHP 之外的变量”以获取关于在 PHP 中使用表单的详细信息及范例。以下是 HTML 表单的范例:
例子 2-6. 一个简单的 HTML表单 <form action="action.php" method="POST">
Your name: <input type="text" name="name" />
Your age: <input type="text" name="age" />
<input type="submit">
</form> |
|
该表单中并没有什么特殊的地方,其中没有使用任何特殊的标识符。当用户填写了该表单并点击了提交按钮,页面 action.php 将被调用。在该文件中,您可以加入如下内容:
例子 2-7. 打印来自表单的数据 |
Hi <?php echo $_POST["name"]; ?>.
You are <?php echo $_POST["age"]; ?> years old.
|
该脚本的输出可能是:
Hi Joe.
You are 22 years old. |
|
该脚本进行的工作应该已经很明显了,这儿并没有其它更复杂的内容。PHP 将自动为您设置 $_POST["name"] 和 $_POST["age"] 变量。在这之前我们使用了自动全局变量 $_SERVER,现在我们引入了自动全局变量 $_POST,它包含了所有的 POST 数据。请注意我们的表单提交数据的方法(method)。如果我们能使用了 GET 方法,那么表单中的信息将被储存到自动全局变量 $_GET 中。如果您并不关心请求数据的来源,您也可以用自动全局变量 $_REQUEST,它包含了所有 GET、POST、COOKIE 和 FILE 的数据。请参阅 import_request_variables() 函数。
RobertMaas at YahooGroups dot Com
31-Aug-2003 01:48
Regarding debate over GET and POST method: One disadvantage of the POST
method is that you can't bookmark it in a URL. So if you want ot make a
URL that bookmarks both the WebPage and some form contents, you have
to use the GET method, i.e. webpage?formcontents. Warning, the Apache
server logs the complete URL including formcontents in a file that
anyone can read, so be sure never to include a password in the form in
GET method, regardless of whether bookmarked or onetime.
For
example, my CGI site has a login form, and I have a bookmark for
specifying my own (rather long) e-mail address, but do *not* include my
password in that bookmark, rather I click on the link and get the login
form with my e-mail address already filled in but I still have to
type my password, and of course it's a POST instead of GET form when I
then submit it with password. http://shell.rawbw.com/~rem/cgi-bin/
LogForm.cgi?id=RobertMaas@YahooGroups.Com (Cliki complained it was too
long so I had to split it.)
| |