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

PHP路由致表单提交空白页、邮件发送失败问题求助

表单提交异常与路由问题排查

问题现象

  • 表单提交后跳转至thank-you.php,但页面空白(开发者工具显示body无HTML内容),邮件无法送达目标邮箱
  • 在thank-you.php中执行var_dump($_POST)无输出
  • 路由测试时,其他子页面仅打印到对应路由位置的数字,而thank-you.php会打印所有路由前的数字(包括后续路由的数字)

相关代码

HTML表单代码

<form class="contact-form" method="POST" action="/thank-you">
    <input type="text" id="website" name="website" />

    <div class="contact-wrapper name">
        <label for="name">
            <?php echo $lang["contact-name"] ?>
        </label>
        <input type="text" id="name" name="name" placeholder="<?php echo $lang['name-placeholder'] ?>" required>
    </div>
    <div class="contact-wrapper email-and-phone">
        <div>
            <label for="email">E-mail</label>
            <input type="text" id="email" name="email" placeholder="<?php echo $lang['email-placeholder'] ?>" required>
        </div>

        <div>
            <label for="telephone">
                <?php echo $lang["contact-telephone"] ?>
            </label>
            <input type="text" id="telephone" name="telephone" placeholder="<?php echo $lang['telephone-placeholder'] ?>">
        </div>
    </div>
    <div class="contact-wrapper subject">
        <label for="subject">
            <?php echo $lang["contact-subject"] ?>
        </label>
        <input type="text" id="subject" name="subject" placeholder="<?php echo $lang['subject-placeholder'] ?>" required>
    </div>

    <div class="contact-wrapper message">
        <label for="message">
            <?php echo $lang["contact-message"] ?>
        </label>
        <textarea id="message" name="message" placeholder="<?php echo $lang['message-placeholder'] ?>" style="height:200px" required></textarea>
    </div>
    <input type="submit" id="submit" name="submit" value="<?php echo $lang['contact-submit'] ?>">
</form>

thank-you.php代码

<?php

// exit if accessed directly.
defined('ABSPATH') || die(header('location: /home'));

if (isset($_POST['submit']) && $_POST['submit'] != '' && $_POST['submit'] != null) {

    $name = $_POST['name'];
    $mailFrom = $_POST['email'];
    $telephone = $_POST['telephone'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];

    $mailTo = "myemailaddress@gmail.com";
    $txt = "You have received an e-mail from " . $name . ".

" . "E-mail: " . $mailFrom . ".

" . "Telephone: " . $telephone . ".

" . $message;

    mail($mailTo, $subject, $txt);
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset='UTF-8' />
    <title>Thank you!</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body>
    <div class="thank-you-wrapper">
        <p>Thank you for reaching out to us. We will get back to you shortly. Please click <a href="/home" style="text-decoration: none; color: #0986d9;">here </a> if you are not redirected within a few seconds.
</p>
    </div>
    <script>
        setTimeout(function() {
            window.location.replace("/home"); // will redirect to main page
        }, 3000); // will call the function after 3 secs.
    </script>
</body>

</html>

路由代码

<?php

require_once __DIR__ . '/router.php';

// Static GET
// In the URL -> http://localhost

print '1';
// The output -> Index
get('/', 'pages/index.php');

print '2';
// The output -> Thank You
get('/thank-you', 'pages/thank-you.php');

print '3';
// The output -> About
get('/about', 'pages/about.php');

print '4';
// The output -> Services
get('/services', 'pages/services.php');

print '5';
// The output -> Web
get('/web', 'pages/web.php');

print '6';
// The output -> Contact
get('/contact', 'pages/contact.php');

问题原因分析

  1. 路由未处理POST请求:表单使用POST方法提交到/thank-you,但路由中仅定义了get('/thank-you', ...),导致POST请求无法匹配对应路由,脚本会继续执行后续所有print语句和路由定义,最终未正确加载thank-you.php,这是thank-you.php会打印所有数字的核心原因。
  2. ABSPATH判断中断页面输出:thank-you.php开头的defined('ABSPATH') || die(...),在路由环境下如果ABSPATH未定义,会直接执行die跳转,中断页面HTML内容的输出,导致页面空白。
  3. 路由匹配后未终止脚本:若router.php中的get方法在匹配到路由后未终止脚本(如未加exit/die),后续代码仍会运行,但其他页面是GET请求能匹配路由,而/thank-you的POST请求匹配不到,因此出现差异。

解决方案

  1. 添加POST路由处理:在路由文件中为/thank-you新增POST路由,确保表单提交的POST请求能被正确匹配:
print '2';
// 保留GET路由用于直接访问,新增POST路由处理表单提交
get('/thank-you', 'pages/thank-you.php');
post('/thank-you', 'pages/thank-you.php');
  1. 修复ABSPATH判断逻辑:

    • 方法一:在路由入口文件开头定义ABSPATH:
    define('ABSPATH', __DIR__);
    require_once __DIR__ . '/router.php';
    
    • 方法二:调整thank-you.php中的判断,适配路由环境:
    // 替换原有判断,仅在直接访问该文件时跳转
    if (!defined('ABSPATH') && basename($_SERVER['PHP_SELF']) === basename(__FILE__)) {
        header('location: /home');
        exit;
    }
    
  2. 确保路由匹配后终止脚本:检查router.php中的路由处理逻辑,确保在匹配到路由并加载对应页面后,立即执行exit;终止脚本,避免后续代码继续运行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 08:30:55