PHP无框架表单需求:实现错误提示与POST数据多页面传递
没问题,我来帮你解决这两个问题——显示空字段错误提示,以及在页面间传递POST数据。核心思路是用Session来暂存错误信息和用户输入(因为重定向会丢失POST数据),同时用隐藏域传递确认后的数据到thanks.php。下面是具体的修改方案:
1. 实现空字段错误提示与数据回填
首先我们需要修改表单页面(也就是你的input.php,建议改名为index.php更直观),通过Session读取错误信息并显示,同时回填用户之前输入的内容;然后修改check.php,在验证失败时把错误和输入数据存入Session再重定向。
修改后的index.php(原input.php)
<?php // 启动Session,必须放在HTML输出前 session_start(); // 从Session获取错误信息,没有则设为空数组 $errors = $_SESSION['errors'] ?? []; // 获取用户之前输入的数据,没有则设为空字符串 $lastName = $_SESSION['last_name'] ?? ''; $firstName = $_SESSION['first_name'] ?? ''; // 用完后清空Session里的临时数据,避免刷新页面重复显示 unset($_SESSION['errors'], $_SESSION['last_name'], $_SESSION['first_name']); ?> <!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Practice1</title> <style> /* 给错误提示加红色样式,更醒目 */ .error { color: #ff4444; margin: 5px 0 0 80px; } </style> </head> <body> <form action="check.php" method="post"> <h1>Input form</h1> <div class="content"> Last Name <input type="text" name="last-name" value="<?php echo htmlspecialchars($lastName); ?>" class=""> <?php if(isset($errors['lastName'])) : ?> <p class="error"><?php echo htmlspecialchars($errors['lastName']); ?></p> <?php endif; ?> </div> <div class="content"> Name <input type="text" name="first-name" value="<?php echo htmlspecialchars($firstName); ?>"> <?php if (isset($errors['firstName'])): ?> <p class="error"><?php echo htmlspecialchars($errors['firstName']); ?></p> <?php endif; ?> </div> <div style="margin-top: 15px;"> <button type="submit">Submit</button> </div> </form> </body> </html>
修改后的check.php
<?php // 启动Session session_start(); if($_SERVER["REQUEST_METHOD"] != "POST"){ header("location: index.php"); exit; } // 获取并过滤POST数据,去掉前后空格 $firstName = trim($_POST['first-name'] ?? ''); $lastName = trim($_POST['last-name'] ?? ''); // 验证字段是否为空,收集错误信息 $errors = []; if (empty($lastName)) { $errors['lastName'] = '※required'; } if (empty($firstName)) { $errors['firstName'] = '※required'; } // 如果有错误,存入Session后重定向回表单页 if (!empty($errors)) { $_SESSION['errors'] = $errors; $_SESSION['last_name'] = $lastName; $_SESSION['first_name'] = $firstName; header("location: index.php"); exit; } // 验证通过,显示确认页面 ?> <!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>confirm</title> <link rel="stylesheet" href="css/reset.css"> <link rel="stylesheet" href="css/style.css"> </head> <body> <form action="thanks.php" method="post"> <h1>confirm</h1> <div class="content"> <p>Last Name: <?php echo htmlspecialchars($lastName) ?></p> </div> <div class="content"> <p>Name: <?php echo htmlspecialchars($firstName) ?></p> </div> <!-- 用隐藏域把数据传递到thanks.php --> <input type="hidden" name="last-name" value="<?php echo htmlspecialchars($lastName); ?>"> <input type="hidden" name="first-name" value="<?php echo htmlspecialchars($firstName); ?>"> <div style="margin-top: 15px;"> <button type="submit">Confirm</button> <button type="button" onclick="location.href='index.php'">Back</button> </div> </form> </body> </html>
2. 传递数据到thanks.php
上面的check.php已经通过隐藏域把用户数据传递给thanks.php了,下面是thanks.php的示例代码,用来接收并显示数据:
<?php if($_SERVER["REQUEST_METHOD"] != "POST"){ header("location: index.php"); exit; } // 获取传递过来的数据 $lastName = trim($_POST['last-name'] ?? ''); $firstName = trim($_POST['first-name'] ?? ''); ?> <!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Thanks</title> </head> <body> <h1>Thanks!</h1> <p><?php echo htmlspecialchars($lastName); ?> <?php echo htmlspecialchars($firstName); ?> さん、ご入力ありがとうございます。</p> <p><a href="index.php">戻る</a></p> </body> </html>
关键说明
- Session的作用:重定向(
header跳转)会丢失POST数据,Session可以临时存储错误信息和用户输入,回到表单页时能读取这些数据,既显示错误提示,又避免用户重复输入。 - 数据传递到thanks.php:用隐藏域(
<input type="hidden">)把数据包含在确认页的表单里,提交时会自动传递到thanks.php;也可以用Session传递,不过隐藏域更直接,适合确认后提交的场景。 - 安全注意:所有输出到页面的用户输入都用
htmlspecialchars()转义,防止XSS攻击;用trim()去掉前后空格,避免用户输入空格就绕过验证的情况。
内容的提问来源于stack exchange,提问作者Sanae
相关产品推荐
相关产品推荐

