BlaBla.cn

PHP: 处理表单 - Manual

BlaBla.cn

处理表单

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 岁了。

    

Apart from the htmlspecialchars() and (int) parts, it should be obvious what this does. htmlspecialchars() makes sure any characters that are special in html are properly encoded so people can't inject HTML tags or Javascript into your page. For the age field, since we know it is

 
BlaBla.cn

© 2005-2008 BlaBla.cn 版权所有