This repository has been archived on 2025-01-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
luowei_exam/form_test_74_2.php
2025-01-09 11:57:41 +08:00

80 lines
2.7 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>表单提交与现场恢复</title>
</head>
<body>
<?php
// 定义变量,用于回显用户输入的数据
$name = "";
$gender = "";
$courses = [];
$hometown = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// 获取表单数据
$name = isset($_POST['name']) ? htmlspecialchars($_POST['name']) : "";
$gender = isset($_POST['gender']) ? $_POST['gender'] : "";
$courses = isset($_POST['course']) ? $_POST['course'] : [];
$hometown = isset($_POST['hometown']) ? $_POST['hometown'] : "";
}
?>
<!-- 表单 -->
<form action="form_test_74_2.php" method="post">
<!-- 姓名 -->
<label for="name">姓名:</label>
<input type="text" id="name" name="name" value="<?php echo $name; ?>">
<br><br>
<!-- 性别 -->
性别:
<input type="radio" id="male" name="gender" value="1" <?php echo ($gender == "1") ? "checked" : ""; ?>>
<label for="male">男</label>
<input type="radio" id="female" name="gender" value="2" <?php echo ($gender == "2") ? "checked" : ""; ?>>
<label for="female">女</label>
<br><br>
<!-- 选课 -->
选课:
<input type="checkbox" id="computer" name="course[]" value="computer" <?php echo in_array("computer", $courses) ? "checked" : ""; ?>>
<label for="computer">计算机</label>
<input type="checkbox" id="math" name="course[]" value="math" <?php echo in_array("math", $courses) ? "checked" : ""; ?>>
<label for="math">数学</label>
<input type="checkbox" id="english" name="course[]" value="english" <?php echo in_array("english", $courses) ? "checked" : ""; ?>>
<label for="english">英语</label>
<br><br>
<!-- 籍贯 -->
籍贯:
<select id="hometown" name="hometown">
<option value="beijing" <?php echo ($hometown == "beijing") ? "selected" : ""; ?>>北京</option>
<option value="chengdu" <?php echo ($hometown == "chengdu") ? "selected" : ""; ?>>成都</option>
<option value="chongqing" <?php echo ($hometown == "chongqing") ? "selected" : ""; ?>>重庆</option>
</select>
<br><br>
<!-- 提交按钮 -->
<input type="submit" id="ok" name="ok" value="提交">
</form>
<!-- 输出提交结果 -->
<?php if ($_SERVER["REQUEST_METHOD"] == "POST"): ?>
<h3>提交结果:</h3>
<p>姓名:<?php echo $name; ?></p>
<p>性别:<?php echo ($gender == "1") ? "男" : (($gender == "2") ? "女" : "未选择"); ?></p>
<p>选课:
<?php
if (!empty($courses)) {
echo implode("", $courses);
} else {
echo "未选择";
}
?>
</p>
<p>籍贯:<?php echo !empty($hometown) ? $hometown : "未选择"; ?></p>
<?php endif; ?>
</body>
</html>