单击HTML提交按钮时返回“ HTTP405”错误

时间:2019-07-02 10:12:16

标签: php html phpmailer

我目前正在通过VisualStudio在本地实时服务器上运行我的网站。 当我单击HTML表单中的“提交”按钮时,我希望我的HTML联系人表单(写在index.html文件中)使用PHPMailer(写在contact-form-handler.php中)发送邮件。

当我点击按钮时,我被带到一个页面,上面写着 “此页面无法使用HTTP 405”。

有人可以向我解释为什么会发生这种情况,以及如何解决它。

index.html:

<form id="contact-form" method="post" action="">
       <label for="name">NAME</label>
       <input id="name" type="text" name="name" required>
       <label for="mob">MOBILE NUMBER</label>
       <input id="mob" type="text" name="mob" required>
       <label for="email">EMAIL</label>
       <input id="email" type="email" name="email" required>
       <label for="message">MESSAGE</label>
       <textarea id="message" type="text" name="message" required></textarea>
       <input class="submit-button" type="submit" value="Submit" name="submit"/>
</form> 

contact-form-handler.php:

<?php
if (isset($_POST['submit'])) {
    require_once('PHPMailer/PHPMailerAutoload.php');
    $mail = new PHPMailer;

    $mail->Host = 'smtp.gmail.com';
    $mail->Port = '587';
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = 'ssl';
    $mail->Username = 'myemailaddress@gmail.com';
    $mail->Password = 'mypassword';

    $mail->setFrom($_POST['email'], $_POST['name']);
    $mail->addAddress= 'myemailaddress@gmail.com';

    $mail->isHTML(true);
    $mail->Subject = 'Test';
    $mail->Body= '<h1> name:'.$_POST['name'].'<br> Email: '.$_POST['email'].'<br> Mobile: '.$_POST['mob'].'<br> message: '.$_POST['message'].'</h1>';

    if(!$mail->send()){
        $result="something went wrong. Please try again."; 
    }
    else{
        $result="Success!"
    }
}

?>

2 个答案:

答案 0 :(得分:0)

HTTP 405 error意味着您使用了不受支持的方法来请求资源,因此出于某种原因,您的Web服务器不允许向端点发送POST请求。您可以尝试将表单标签更改为:

<form id="contact-form" method="get" action="">

这不是一个很好的解决方案-更好的方法是修复服务器配置以允许POST方法。

答案 1 :(得分:-1)

我会在您的问题下发表评论,但我不能。 @RiggsFolly是正确的,您必须放置action属性php文件,因为表单必须将其发送到您的contact-form-handler.php。请确保您没有在index.html中使用php代码,因为它不起作用。如果您从contact-form-handler.php中添加问题代码,那会很好,也许可以帮助我们找出问题所在