PHP 超全局变量 $_GET 和 $_POST 用于收集表单数据(form-data)。
Post方法
PHP - 一个简单的 HTML 表单
下面的例子显示了一个简单的 HTML 表单,它包含两个输入字段和一个提交按钮:
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
welcome.php文件
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
输出:
Welcome Bill
Your email address is Bill.Gates@example.com
GET 方法
<html>
<body>
<form action="welcome_get.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
"welcome_get.php" 是这样的:
<html>
<body>
Welcome <?php echo $_GET["name"]; ?><br>
Your email address is: <?php echo $_GET["email"]; ?>
</body>
</html>
上面的代码很简单。不过,最重要的内容被漏掉了。您需要对表单数据进行验证,以防止脚本出现漏洞。
注意:在处理 PHP 表单时请关注安全!