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

PHP带验证联系表单发送成功无提示且报页面未找到问题求助

解决PHP联系表单成功发送后无提示且页面未找到的问题

我来帮你排查并修复这个问题,咱们一步步梳理:

核心问题分析

  1. 跳转目标错误:你当前在处理提交逻辑的代码里,成功发送邮件后跳转到了index.php?success,但你的成功提示代码是写在contact.php里的;如果index.php不存在或者路径不正确,就会直接触发“页面未找到”错误。
  2. 邮件头格式不规范:mail()函数的第四个参数需要符合标准邮件头格式,直接传邮箱地址存在潜在风险(虽然你说邮件发送成功了,但规范写法能避免后续问题)。
  3. 代码文件拆分混乱:看起来你可能把提交处理逻辑错误地混在了contact.php里,导致表单提交的目标文件send.php实际不存在,这也是页面未找到的常见诱因。

修复后的完整代码

1. 正确的contact.php代码

这个文件只负责显示表单和处理错误/成功提示:

<div class="container"> 
  <div class="row text-center"> 
    <div class="col-12 heading"> 
      <h1>Contact Us</h1> 
    </div> 
    <div class="col-12"> 
      <?php 
        $Msg = ""; 
        // 空字段错误提示(修正了拼写错误:Blannks → Blanks)
        if(isset($_GET['error'])) { 
          $Msg = "Please Fill in All Blanks"; 
          echo '<div class="alert alert-danger">'.$Msg.'</div>'; 
        } 
        // 发送成功提示
        if(isset($_GET['success'])) { 
          $Msg = "Your Message Has Been Sent"; 
          echo '<div class="alert alert-success">'.$Msg.'</div>'; 
        }
        // 可选:添加邮件发送失败的提示
        if(isset($_GET['mailerror'])) {
          $Msg = "Sorry, we couldn't send your message. Please try again later.";
          echo '<div class="alert alert-warning">'.$Msg.'</div>';
        }
      ?> 
    </div> 
  </div> 
</div> 
<div class="container"> 
  <div class="row text-center"> 
    <div class="col-12"> 
      <form class="reportform" action="send.php" method="post"> 
        <input type="text" name="UName" placeholder="Full Name"><br> 
        <input type="email" name="Email" placeholder="Your e-mail"><br> 
        <input type="text" name="Subject" placeholder="Subject"><br> 
        <textarea name="msg" placeholder="Message"></textarea><br> 
        <h6>By clicking send button, you agree our <a target="_blank" href="../credits/ppolicy.html">Privacy Policy</a></h6> 
        <button type="submit" name="btn-send" class="btn btn-primary sendbtn">Send</button> 
      </form> 
    </div> 
  </div> 
</div>

2. 正确的send.php代码

这个文件专门处理表单提交逻辑,要和contact.php放在同一目录下:

<?php
if(isset($_POST['btn-send'])){
  // 修剪字段前后空格,避免用户用空格绕过空验证
  $UserName = trim($_POST['UName']);
  $Email = trim($_POST['Email']);
  $Subject = trim($_POST['Subject']);
  $Msg = trim($_POST['msg']);

  // 验证字段是否为空
  if(empty($UserName) || empty($Email) || empty($Subject) || empty($Msg)) {
    header('location: contact.php?error');
    exit; // 跳转后必须exit,防止后续代码继续执行
  } else {
    $to = "nika.makhatadze17@gmail.com";
    // 修复邮件头格式,符合邮件协议规范
    $headers = "From: $Email\r\n";
    $headers .= "Reply-To: $Email\r\n";
    $headers .= "Content-Type: text/plain; charset=UTF-8\r\n";

    if(mail($to, $Subject, $Msg, $headers)) {
      // 成功后跳回contact.php显示成功提示
      header("location: contact.php?success");
      exit;
    } else {
      // 邮件发送失败时的跳转提示
      header("location: contact.php?mailerror");
      exit;
    }
  }
} else {
  // 如果直接访问send.php,跳回contact.php
  header("location: contact.php");
  exit;
}
?>

关键修复点说明

  • 跳转目标修正:把成功后的跳转从index.php?success改成contact.php?success,这样就能触发你原本写在contact.php里的成功提示了。
  • 邮件头规范:添加了标准的From、Reply-To和Content-Type头,确保邮件发送更稳定,也符合邮件协议要求。
  • 添加exit语句:跳转后必须加上exit或die(),防止PHP继续执行后续代码,避免潜在的逻辑错误。
  • 代码拆分:把表单显示和提交逻辑分开到两个文件,既清晰又避免代码混乱。
  • 字段修剪:用trim()处理输入字段,防止用户输入空格绕过空字段验证。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 09:04:37