PHP表单提交后无法显示错误提示及回显数据问题求助
Hey there! Let's troubleshoot why your PHP form isn't showing error messages or echoing submitted data. Based on the snippet you shared, here are the most common fixes to get things working:
First, you need to ensure your validation code only runs when the form is submitted via POST. Without this check, your error variables won't populate when the form loads initially, and the logic won't trigger on submission.
Wrap your validation logic in this check:
<?php $nameErr = ""; $name = ""; // Trigger code only when form is submitted if ($_SERVER["REQUEST_METHOD"] == "POST") { // Validation logic goes here } ?>
Your current span has static &... text—you need to echo an error variable that gets set when validation fails. Here's how to implement that:
First, define an error variable and set it based on validation rules:
<?php $nameErr = ""; $name = ""; // Helper function to sanitize input function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["name"])) { $nameErr = "Name is required"; } else { $name = test_input($_POST["name"]); // Optional: Add extra validation (e.g., only letters and spaces) if (!preg_match("/^[a-zA-Z-' ]*$/", $name)) { $nameErr = "Only letters and white space are allowed"; } } } ?>
Then update your span to echo the error variable:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <label> <input type="text" placeholder="Enter fullname here" name="name" value="<?php echo $name; ?>"> <span class="err"><?php echo $nameErr; ?></span> </label> <button type="submit">Submit</button> </form>
We also added value="<?php echo $name; ?>" to the input—this keeps the submitted value in the field if validation fails, improving user experience.
To display the validated data, add this somewhere after the form:
<?php if (!empty($name) && empty($nameErr)) { ?> <p>Your submitted name is: <?php echo $name; ?></p> <?php } ?>
If none of the above works, enable error reporting to catch syntax issues or undefined variables. Add this at the very top of your PHP file:
<?php error_reporting(E_ALL); ini_set('display_errors', 1); ?>
This will show any PHP errors on the page, pointing you directly to the problem.
Sometimes the issue is that PHP isn't configured correctly. Test this by creating a file called phpinfo.php with:
<?php phpinfo(); ?>
Visiting this file should show a PHP information page. If it displays raw code, you need to fix your server's PHP setup.
Full Working Example
Putting it all together, your complete code would look like this:
<?php error_reporting(E_ALL); ini_set('display_errors', 1); $nameErr = ""; $name = ""; function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["name"])) { $nameErr = "Name is required"; } else { $name = test_input($_POST["name"]); if (!preg_match("/^[a-zA-Z-' ]*$/", $name)) { $nameErr = "Only letters and white space are allowed"; } } } ?> <!DOCTYPE html> <html> <body> <h2>Form Validation</h2> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <label> Full Name: <input type="text" placeholder="Enter fullname here" name="name" value="<?php echo $name; ?>"> <span class="err"><?php echo $nameErr; ?></span> </label> <br><br> <button type="submit">Submit</button> </form> <?php if (!empty($name) && empty($nameErr)) { ?> <h3>Your Input:</h3> <p>Name: <?php echo $name; ?></p> <?php } ?> </body> </html>
内容的提问来源于stack exchange,提问作者pedroyanky

