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

网站订阅表单后端报错求助:Apache服务器提示请求URL未找到

问题描述

我正在为网站制作可用的订阅表单,HTML部分没问题,但后端遇到故障。我不太熟悉PHP和JS,仍尝试让代码运行,提交表单时出现报错:

Not Found
The requested URL was not found on this server.

Apache/2.4.38 (Debian) Server at 35.232.170.255 Port 80

我曾在w3schools的PHP代码运行器中运行以下代码:

<?php 
    $message_sent = false;
    if(isset($_POST['email']) && $_POST['email'] != ''){

        if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ){

            //submit the form
            $userName = $_POST['name'];
            $userEmail = $_POST['email'];
            $messageSubject = $_POST['subject'];
            $message = $_POST['message'];

            $to = 'drippostaff@gmail.com';
            $body = "";

            $body = "From: ".$userName. "\r\n";
            $body = "Email: ".$userEmail. "\r\n";
            $body = "Message: ".$message. "\r\n";

            mail($to,$messageSubject,$body);

            $message_sent = true;        
        }
            
        else{
            $invalid_class_name = "form-invalid";
        }


}

?>

<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="webform.css" media="all">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>    
    <script>
        $(function() {
   
   $(".form-control").on('focus', function(){

       $(this).parents(".form-group").addClass('focused');

   });

   $(".form-control").on('blur', function(){

       $(this).parents(".form-group").removeClass('focused');

   });

});
    </script>
</head>
<body>
    <div class="container">
        <form action="webform.php" method="POST" class="form">
            <div class="form-group">
                <label for="name" class="form-label">Your Name</label>
                <input type="text" class="form-control" id="name" name="name" placeholder="Jane Doe" tabindex="1" required>
            </div>
            <div class="form-group">
                <label for="email" class="form-label">Your Email</label>
                <input type="email" class="form-control" id="email" name="email" placeholder="jane@doe.com" tabindex="2" required>
            </div>
            <div class="form-group">
                <label for="subject" class="form-label">Subject</label>
                <input type="text" class="form-control" id="subject" name="subject" placeholder="Hello There!" tabindex="3" required>
            </div>
            <div class="form-group">
                <label for="message" class="form-label">Message</label>
                <textarea class="form-control" rows="5" cols="50" id="message" name="message" placeholder="Enter Message..." tabindex="4"></textarea>
            </div>
            <div>
                <button type="submit" class="btn">Send Message!</button>
            </div>
        </form>
    </div>
</body>
</html>

解决方法

  1. 修正表单提交路径
    表单action属性设为webform.php,但如果当前文件就是webform.php,直接提交到自身即可,将action改为空字符串,或者action="<?php echo $_SERVER['PHP_SELF']; ?>",避免因找不到目标文件触发404错误。

  2. 检查文件命名与路径

    • 确认PHP文件确实命名为webform.php,且与相关资源(如webform.css)放在服务器网站根目录(Debian系统通常为/var/www/html/)下。
    • 核对服务器文件目录,确保webform.php存在于Apache配置的网站根目录中。
  3. 修复邮件内容拼接错误
    当前代码中$body每次赋值都会覆盖之前内容,改用.=拼接,保证邮件包含所有信息:

    $body = "From: ".$userName. "\r\n";
    $body .= "Email: ".$userEmail. "\r\n";
    $body .= "Message: ".$message. "\r\n";
    
  4. 添加邮件头信息
    默认mail()函数发送的邮件易被判定为垃圾邮件,补充必要的邮件头:

    $headers = "From: ".$userName." <".$userEmail.">\r\n";
    $headers .= "Reply-To: ".$userEmail."\r\n";
    $headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
    mail($to, $messageSubject, $body, $headers);
    
  5. 配置服务器邮件功能
    若修正后仍无法收到邮件,需确认服务器已配置PHP邮件发送环境,Debian系统可安装sendmail包来支持邮件发送。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 10:21:13