处理表单

PHP 一个很有用的特点体现在它处理 PHP 表单的方式。需要理解的非常重要的原理,是表单的任何元素都在 PHP 脚本中自动生效。请参阅本手册中“PHP 的外部变量”以获取关于在 PHP 中使用表单的详细信息及范例。以下是 HTML 表单的范例:

例子 2-6. 一个简单的 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 将被调用。在该文件中,可以加入如下内容:

例子 2-7. 打印来自表单的数据

你好,<?php echo $_POST['name']; ?>
<?php echo $_POST['age']; ?> 岁了。

该脚本的输出可能是:

你好,Joe。你 22 岁了。

该脚本进行的工作应该已经很明显了,这儿并没有其它更复杂的内容。PHP 将自动设置 $_POST['name']$_POST['age'] 变量。在这之前我们使用了自动全局变量 $_SERVER,现在我们引入了自动全局变量 $_POST,它包含了所有的 POST 数据。请注意我们的表单提交数据的方法(method)。如果使用了 GET 方法,那么表单中的信息将被储存到自动全局变量 $_GET 中。如果并不关心请求数据的来源,也可以用自动全局变量 $_REQUEST,它包含了所有 GET、POST、COOKIE 和 FILE 的数据。请参阅 import_request_variables() 函数。

也可以在 PHP 中处理 XForms 的输入,尽管可能更喜欢使用长久以来支持良好的 HTML 表单。XForms 目前还不适合初学者使用,但是用户可能对它感兴趣。手册中在“特点”一章有一节对如何处理从 XForum 接收到的数据进行了简短的介绍。


add a note add a note User Contributed Notes
evlooij at xs4all dot nl
20-Sep-2006 12:36
I agree that the manual is not very clear on how one should structure the files, but it is not true that the action attribute of a form always points to a different file: it can also point to the same file. Say you have a file called myForm.php with the following code:

<html>
<body>
<form method="post" action="action.php" target="_self">
<button type="submit" name="actionToTake" value="Hello there!">Say hello</button>
</form>
</body>
</html>

Then you would indeed need to create a file named action.php and have it do something like <?php echo "<html><body>".$_POST['actionToTake']."</body></html>"; ?>

But you'll often find that it is handier to let myForm.php handle your user's input, so then you would set the action to the current page, and for that PHP has a nice variable $PHP_SELF to help you:

<html>
<body>
<?php echo "actionToTake=".$_POST['actionToTake']."\n" ?>
<form method="post" action="<?php echo $PHP_SELF ?>" target="_self">
<button type="submit" name="actionToTake" value="Hello there!">Say hello</button>
</form>
</body>
</html>

Hope this helps you get started.
yasman at phplatvia dot lv
05-May-2005 03:18
[Editor's Note: Since "." is not legal variable name PHP will translate the dot to underscore, i.e. "name.x" will become "name_x"]

Be careful, when using and processing forms which contains
<input type="image">
tag. Do not use in your scripts this elements attributes `name` and `value`, because MSIE and Opera do not send them to server.
Both are sending `name.x` and `name.y` coordiante variables to a server, so better use them.
sethg at ropine dot com
02-Dec-2003 04:55
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.