You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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:

1. Add a Form Submission Check

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
}
?>
2. Populate the Error Span Dynamically

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.

3. Echo Submitted Data After Successful Validation

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 } ?>
4. Check for Hidden PHP Errors

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.

5. Verify PHP is Running on Your Server

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:12:43