PHP 表单 - 必需字段

Which fields are required, how to mark them, and how to report errors kindly.

Beginner
Key PointsRequired means non-empty after trimming — use `!== ''`, not `empty()`.

Marking required fields

Use the required attribute for client hints and a * in the label. The server must still check.

HTML
<form method="post" action="submit.php">
    <label>姓名 <span style="color:red">*</span></label>
    <input type="text" name="name" required><br>

    <label>邮箱 <span style="color:red">*</span></label>
    <input type="text" name="email" required><br>

    <label>网址</label>
    <input type="text" name="website"><br>

    <label>评论</label>
    <textarea name="comment"></textarea><br>

    <label>性别 <span style="color:red">*</span></label>
    <input type="radio" name="gender" value="female">女
    <input type="radio" name="gender" value="male">男<br>

    <input type="submit" value="提交">
</form>

Required validation

PHP
<?php
$fields = [
    "name"    => "姓名",
    "email"   => "邮箱",
    "gender"  => "性别",
];
$optional = ["website", "comment"];

$errors = [];
$data = [];

foreach ($fields as $key => $label) {
    $v = trim($_POST[$key] ?? "");
    if ($v === "") {
        $errors[$key] = "$label 是必填项";
    } else {
        $data[$key] = $v;
    }
}

foreach ($optional as $key) {
    $data[$key] = trim($_POST[$key] ?? "");
}

if ($errors) {
    foreach ($errors as $e) {
        echo "错误: $e\n";
    }
} else {
    echo "全部通过\n";
    print_r($data);
}
Output
全部通过
Array
(
    [name] => Tom
    [email] => tom@example.com
    [gender] => male
    [website] => 
    [comment] => 
)
Normalise optional fields into $data too, so downstream code never checks key existence. A uniform shape beats saving lines.

Rendering errors back

On failure keep what the user typed. Echo $data back into value so only the wrong field needs editing.

PHP
<?php
$errors = ["email" => "邮箱 是必填项"];
$data   = ["name" => "Tom", "email" => ""];

function val(array $d, string $k): string {
    return htmlspecialchars($d[$k] ?? "", ENT_QUOTES, "UTF-8");
}
?>
<form method="post">
    <input name="name" value="<?= val($data, "name") ?>">
    <?php if (isset($errors["name"])): ?>
        <span class="err"><?= val($errors, "name") ?></span>
    <?php endif; ?>

    <input name="email" value="<?= val($data, "email") ?>">
    <?php if (isset($errors["email"])): ?>
        <span class="err"><?= val($errors, "email") ?></span>
    <?php endif; ?>

    <button>提交</button>
</form>
Output
<form method="post">
    <input name="name" value="Tom">
    

    <input name="email" value="">
        <span class="err">邮箱 是必填项</span>
    

    <button>提交</button>
</form>

Always pass all three arguments: ENT_QUOTES escapes single quotes too, UTF-8 pins the charset.

Required is not valid

Filled and correct are two gates, in this order:

  1. Required —— trim($v) !== ''
  2. Format —— email, URL, number, date
  3. Range —— length, numeric bounds, allowed values
  4. Business —— username taken, coupon valid

Steps one and two belong to the form layer; three and four usually hit the database and belong in a service.

Friends & Partners