当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > PHP 表单

PHP
WINDOWS下安装MySQL
PHP 制作 网站/服务器 监视脚本
用PHP和CSS制作活动按钮
PHP 单件模式
PHP MVC模式,类封装以及HACK
PHP 中使用正则表达式
PHP 防止 SQL 注入攻击
PHP 跨站点脚本攻击
PHP 防止用户操纵 GET 变量
PHP 防止远程表单提交

PHP 表单


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 1408 ::
收藏到网摘: n/a

The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input.
PHP $_GET 和 $_POST变量是用来获取表单中的信息的,比如用户输入的信息。


PHP Form Handling
PHP表单操作

The most important thing to notice when dealing with HTML forms and PHP is that any form element in an HTML page will automatically be available to your PHP scripts.
在我们处理HTML表单和PHP表单时,我们要记住的重要一点是:HTML页面中的任何一个表单元素都可以自动的用于PHP脚本:

Form example:
表单举例:

<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>

The example HTML page above contains two input fields and a submit button. When the user fills in this form and click on the submit button, the form data is sent to the "welcome.php" file.
上述HTML页面包含了两个输入框[input field]和一个提交[submit]按钮。当用户将信息填写完毕并点击提交按钮时,表单的数据将被发送至“welcome.php”文件。

The "welcome.php" file looks like this:
“welcome.php”文件如下所示:

<html>
<body>
Welcome <?php echo $_POST["name"]; ?>.<br />
You are <?php echo $_POST["age"]; ?> years old.
</body>
</html>

A sample output of the above script may be:
上述脚本将输出下面这段结果:

Welcome John.
You are 28 years old.

The PHP $_GET and $_POST variables will be explained in the next chapters.
PHP $_GET 和 $_POST变量将在下一章作具体讲解。


Form Validation
表单有效性验证[Form Validation]

User input should be validated on the browser whenever possible (by client scripts (JavaScript)). Browser validation is faster and you reduce the server load.
用户输入的信息应该尽可能的通过客户端脚本程序(如:JavaScript)浏览器上验证;通过浏览器进行信息的有效性验证可以提高效率并减少服务器的下载压力。

You should consider using server validation if the user input will be inserted into a database. A good way to validate a form on the server is to post the form to itself, instead of jumping to a different page. The user will then get the error messages on the same page as the form. This makes it easier to discover the error.
如果用户输入的信息需要存进数据库,那么你必须考虑在服务器端进行有效性验证。在服务器上验证信息有效性的最好方法就是把表单信息发给当前页进行验证,而不是调到其他页面进行验证。通过上述方法,如果表单存在错误,用户可以直接在当前页获取错误信息。这使得我们更容易发现存在的错误信息。

评论 (1) 1 All

登陆 | 还没注册?