PHP 表单
How an HTML form hands data to PHP, and the minimal handling loop.
A minimal form
The form page is plain HTML; another PHP script receives the submission. action names the receiver, method picks get or post.
<form action="welcome.php" method="post">
名字: <input type="text" name="fname"><br>
年龄: <input type="text" name="age"><br>
<input type="submit" value="提交">
</form>The receiver reads $_POST by name. Check existence first, or a direct visit to welcome.php raises an undefined index warning.
<?php
$name = $_POST["fname"] ?? "";
$age = $_POST["age"] ?? "";
if ($name === "" || $age === "") {
echo "请通过表单提交";
exit;
}
echo "欢迎 " . htmlspecialchars($name) . "<br>";
echo "你的年龄是 " . htmlspecialchars($age) . " 岁。";欢迎 Tom<br>你的年龄是 18 岁。GET vs POST
| Aspect | GET | POST |
|---|---|---|
| Where the data goes | URL 查询串 | 请求体 |
| Visible in URL | 是 | 否 |
| Length limit | 受 URL 长度限制(约 2KB) | 受 post_max_size 限制 |
| Bookmarkable | 可以 | 不可以 |
| Idempotent | 是,刷新安全 | 否,刷新会重复提交 |
| Use for | 查询、分页、筛选 | 登录、上传、写操作 |
Field types
| Input | PHP receives |
|---|---|
| <input type="text"> | 字符串 |
| <input type="checkbox"> | 勾选才有键;多选加 name="x[]" 得数组 |
| <input type="radio"> | 字符串,单个值 |
| <select multiple> | 数组 |
| <textarea> | 字符串(含换行) |
| <input type="file"> | 进 $_FILES,不进 $_POST |
<?php
// 表单: <input type="checkbox" name="hobby[]" value="read">
$hobbies = $_POST["hobby"] ?? [];
echo is_array($hobbies) ? implode(",", $hobbies) : $hobbies, "\n";
echo count($hobbies), "\n";read,music
2Self-submitting forms
Leave action empty so the form posts to itself — the page both renders and handles. The classic PHP utility pattern.
<?php
$submitted = ($_SERVER["REQUEST_METHOD"] ?? "") === "POST";
$msg = "";
if ($submitted) {
$n = (int)($_POST["n"] ?? 0);
$msg = "你输入了 $n,平方是 " . ($n * $n);
}
echo $submitted ? "<p>$msg</p>" : '<form method="post"><input name="n"></form>', "\n";<p>你输入了 7,平方是 49</p>The standard handling pattern
- Check that the request method is POST
- Read
$_POSTwith??defaults trim()the input- Validate type and range, collect errors
- Re-render with errors, or process
- Redirect after processing
The following chapters unpack each step, then assemble them in the complete form example.
一个最小的表单
表单页面是纯 HTML,提交后由另一个 PHP 脚本接收。action 指向接收脚本,method 选 get 或 post。
<form action="welcome.php" method="post">
名字: <input type="text" name="fname"><br>
年龄: <input type="text" name="age"><br>
<input type="submit" value="提交">
</form>接收端用 $_POST 按 name 取值。取之前必须判断存在性,否则用户直接访问 welcome.php 会报 undefined index。
<?php
$name = $_POST["fname"] ?? "";
$age = $_POST["age"] ?? "";
if ($name === "" || $age === "") {
echo "请通过表单提交";
exit;
}
echo "欢迎 " . htmlspecialchars($name) . "<br>";
echo "你的年龄是 " . htmlspecialchars($age) . " 岁。";欢迎 Tom<br>你的年龄是 18 岁。GET 与 POST 的区别
| 对比项 | GET | POST |
|---|---|---|
| 数据位置 | URL 查询串 | 请求体 |
| 是否出现在地址栏 | 是 | 否 |
| 长度限制 | 受 URL 长度限制(约 2KB) | 受 post_max_size 限制 |
| 可否收藏 | 可以 | 不可以 |
| 幂等性 | 是,刷新安全 | 否,刷新会重复提交 |
| 适用场景 | 查询、分页、筛选 | 登录、上传、写操作 |
表单字段类型
| 控件 | PHP 收到 |
|---|---|
| <input type="text"> | 字符串 |
| <input type="checkbox"> | 勾选才有键;多选加 name="x[]" 得数组 |
| <input type="radio"> | 字符串,单个值 |
| <select multiple> | 数组 |
| <textarea> | 字符串(含换行) |
| <input type="file"> | 进 $_FILES,不进 $_POST |
<?php
// 表单: <input type="checkbox" name="hobby[]" value="read">
$hobbies = $_POST["hobby"] ?? [];
echo is_array($hobbies) ? implode(",", $hobbies) : $hobbies, "\n";
echo count($hobbies), "\n";read,music
2自提交表单
把 action 留空,表单提交给自己,在同一页面里既显示表单又处理数据。这是 PHP 最常见的小工具写法。
<?php
$submitted = ($_SERVER["REQUEST_METHOD"] ?? "") === "POST";
$msg = "";
if ($submitted) {
$n = (int)($_POST["n"] ?? 0);
$msg = "你输入了 $n,平方是 " . ($n * $n);
}
echo $submitted ? "<p>$msg</p>" : '<form method="post"><input name="n"></form>', "\n";<p>你输入了 7,平方是 49</p>header("Location: " . $_SERVER["PHP_SELF"]); exit; 重定向一次,这叫 PRG(Post/Redirect/Get)模式。处理表单的固定套路
- 判断请求方法是不是 POST
- 从
$_POST取值,用??给默认值 trim()去掉首尾空白- 校验类型与范围,收集错误信息
- 有错回显表单;无错则处理(入库、发信)
- 处理完重定向,避免重复提交
后面几章会把每一步拆开讲,最后在「PHP 完整表单实例」里拼成一个能用的版本。