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

PHP表单验证失败后页面内容丢失的解决方法

解决表单提交错误后页面元素消失的问题

问题根源:你代码里的problem()函数使用了die(),一旦触发验证错误,PHP脚本会直接停止执行,导致后续的表单、联系方式、页脚等所有HTML内容都不会被输出。

以下是修复后的完整代码,同时还优化了保留用户输入值的功能,避免用户重复填写:

<h2>Contact</h2>

<div id="red-error">
    <?php
    $error_message = ""; // 初始化错误信息变量

    if (isset($_POST['Email'])) {
        // 配置收件邮箱和邮件主题
        $email_to = "ArthurielBoston@gmail.com";
        $email_subject = "Actor Portfolio: email submission";

        // 验证必填字段是否存在
        if (!isset($_POST['Name']) || !isset($_POST['Email']) || !isset($_POST['Message'])) {
            $error_message = 'We are sorry, but there appears to be a problem with the form you submitted.';
        } else {
            $name = $_POST['Name'];
            $email = $_POST['Email'];
            $message = $_POST['Message'];

            // 邮箱格式验证
            $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
            if (!preg_match($email_exp, $email)) {
                $error_message .= 'The Email address you entered does not appear to be valid.<br>';
            }

            // 姓名格式验证
            $string_exp = "/^[A-Za-z .'-]+$/";
            if (!preg_match($string_exp, $name)) {
                $error_message .= 'The Name you entered does not appear to be valid.<br>';
            }

            // 消息长度验证
            if (strlen($message) < 2) {
                $error_message .= 'The Message you entered do not appear to be valid.<br>';
            }

            // 如果没有错误,发送邮件并跳转
            if (empty($error_message)) {
                $email_message = "Form details below.\n";

                function clean_string($string)
                {
                    $bad = array("content-type", "bcc:", "to:", "cc:", "href");
                    return str_replace($bad, "", $string);
                }

                $email_message .= "Name: " . clean_string($name) . "\n";
                $email_message .= "Email: " . clean_string($email) . "\n";
                $email_message .= "Message: " . clean_string($message) . "\n";

                // 创建邮件头
                $headers = 'From: ' . $email . "\r\n" .
                    'Reply-To: ' . $email . "\r\n" .
                    'X-Mailer: PHP/' . phpversion();
                @mail($email_to, $email_subject, $email_message, $headers);

                // 跳转成功页面
                header('Location: https://arthurielbostonactorportfolio.com/successful_submission.html');
                exit; // 跳转后终止脚本,避免后续内容输出
            }
        }
    }

    // 输出错误信息(如果有)
    if (!empty($error_message)) {
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br><br>";
        echo $error_message . "<br><br>";
    }
    ?>
</div>

<h3>Email:</h3>

<form class="form" method="post" action="contact.php">
    <label for="Name" class="emailtext"></label>
    <input type="text" name="Name" placeholder="Name: (ex: John Doe)" value="<?php echo isset($_POST['Name']) ? htmlspecialchars($_POST['Name']) : ''; ?>"/>

    <br>

    <label for="Email" class="emailtext"></label>
    <input type="text" name="Email" placeholder="Email: (ex: JohnDoe@email.com)" value="<?php echo isset($_POST['Email']) ? htmlspecialchars($_POST['Email']) : ''; ?>"/>

    <label for="Message" class="emailtext"></label>
    <textarea type="text" name="Message" class="message" placeholder="Message: (ex: Here is my email message.)"><?php echo isset($_POST['Message']) ? htmlspecialchars($_POST['Message']) : ''; ?></textarea>

    <input type="submit" class="button" value="Submit"/>
</form>

<h3>Phone:</h3> <br>
<p class="paragraph1">+1 ( 610 ) 809-8699</p>
<h3>Avaliable Locations To Work:</h3>
<p class="paragraph1">Deleware County, PA
Philadelphia, PA <br>
King of Prussia, PA <br>
New Jersey <br>
Deleware <br>
New York City, NY </p>
</div>

</body>

<footer>
&copy; 2024 Arthuriel Boston. All rights reserved.
</footer>

</html>

关键改动说明:

  1. 移除die(),用变量存储错误信息:不再直接终止脚本,而是把错误信息存在$error_message里,最后统一输出在错误容器中
  2. 保留用户输入值:给表单的输入框和文本域加上value属性,用htmlspecialchars()转义防止XSS攻击,确保用户提交的内容能保留在表单里
  3. 调整邮件发送后的逻辑:跳转成功页面后用exit终止脚本,避免后续内容输出
  4. 重构验证流程:把错误判断逻辑整合,让代码更清晰

内容的提问来源于stack exchange,提问作者Aokaminariki

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 04:32:48