<form action="handle.php" method="post">
<input type="hidden" name="data[address]" value="Beijing">
<input type="hidden" name="data[age]" value="20">
<button type="submit">提交</button>
</form>
// handle.php
$data = $_POST['data'] ?? [];
echo $data['address']; // Beijing
echo $data['age']; // 20
<form method="post">
<input type="checkbox" name="fruit[]" value="apple"> 蘋果
<input type="checkbox" name="fruit[]" value="pear"> 梨
<input type="checkbox" name="fruit[]" value="banana"> 香蕉
<button>提交</button>
</form>
// PHP 端
$fruits = $_POST['fruit'] ?? []; // ['apple', 'banana']
foreach ($fruits as $f) echo $f . '
';
<?php
echo "POST 數據:
";
foreach ($_POST as $k => $v) {
echo htmlspecialchars("$k => ") . (is_array($v) ? json_encode($v, JSON_UNESCAPED_UNICODE) : htmlspecialchars($v)) . "
";
}
echo "
FILES 數據:
";
print_r($_FILES);
前端 JS 動態添加 <input type="file" name="files[]" multiple> (HTML5 multiple 屬性),PHP 端遍歷 $_FILES['files']:
<?php
foreach ($_FILES['files']['tmp_name'] as $idx => $tmp) {
if ($_FILES['files']['error'][$idx] !== UPLOAD_ERR_OK) continue;
$name = basename($_FILES['files']['name'][$idx]);
$dest = "uploads/" . uniqid() . '_' . $name;
move_uploaded_file($tmp, $dest);
echo "上傳成功: $dest
";
}
?>
finfo_file() 檢測 MIME,不信任擴展名。php.ini upload_max_filesize + 代碼二次檢查。location ~ \.php$ { deny all; }。htmlspecialchars() 防 XSS。