不能POST /index.php

时间:2016-06-28 02:11:03

标签: php html forms email post

我是php的新手,我试图通过联系表单发送到我的电子邮箱。每次我点击提交按钮,它都会将我重定向到一个页面,上面写着“不能发布/index.php'”。我尝试从localhost运行它并将其部署到heroku,仍然得到相同的错误。 html和php文件位于同一个文件夹中。

控制台指出以下错误:'无法加载资源:服务器响应状态为404(未找到)'。

表单的HTML代码是

<form name="sentMessage" role="form" id="contactForm" novalidate name="contactform" method="post" action="index.php">
                <div class="control-group form-group">
                    <div class="controls">
                        <label for="first_name">Full Name:</label>
                        <input type="text" class="form-control" name="name" id="name" required data-validation-required-message="Please enter your name.">
                        <p class="help-block"></p>
                    </div>
                </div>
                <div class="control-group form-group">
                    <div class="controls">
                        <label for="telephone">Phone Number:</label>
                        <input type="tel" class="form-control" id="phone" name="telephone" required data-validation-required-message="Please enter your phone number.">
                    </div>
                </div>
                <div class="control-group form-group">
                    <div class="controls">
                        <label for="email">Email Address:</label>
                        <input type="email" class="form-control" name="email" id="email" required data-validation-required-message="Please enter your email address.">
                    </div>
                </div>
                <div class="control-group form-group">
                    <div class="controls">
                        <label for="comments">Message:</label>
                        <textarea rows="10" cols="100" class="form-control" name="message" id="message" required data-validation-required-message="Please enter your message" maxlength="999" style="resize:none"></textarea>
                    </div>
                </div>
                <div id="success"></div>
                <!-- For success/fail messages -->
                <button type="submit" class="btn btn-primary" id="send-email">Send Message</button>
                <div class="form-group">
                    <div class="col-sm-10 col-sm-offset-2">
                        <?php echo $result; ?>  
                    </div>
                </div>
            </form>

PHP文件是:

<?php
if (isset($_POST["submit"])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $telephone = $_POST['telephone'];
    $from = 'Demo Contact Form'; 
    $to = 'example@bootstrapbay.com'; 
    $subject = 'Message from Contact Demo ';

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    // Check if name has been entered
    if (!$_POST['name']) {
        $errName = 'Please enter your name';
    }

    //Check if simple anti-bot test is correct
    if (!$_POST['telephone']) {
        $errName = 'Please enter your telephone number';
    }

    // Check if email has been entered and is valid
    if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $errEmail = 'Please enter a valid email address';
    }

    //Check if message has been entered
    if (!$_POST['message']) {
        $errMessage = 'Please enter your message';
    }

// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
    if (mail ($to, $subject, $body, $from)) {
        $result='<div class="alert alert-success">Thank You! I will be in      touch</div>';
 } else {
    $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
    }
?>

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

您需要设置提交按钮的名称,因为您在PHP中检查它。

if (isset($_POST["submit"])) {

首先添加如下:

 <button type="submit" name="submit" class="btn btn-primary" id="send-email">Send Message</button>

你还需要检查php是否在index.php中,你指示表单发送数据。

我认为这是一个拼写错误(可能是复制/粘贴拼写错误)

 if (!$_POST['telephone']) {
        $errName = 'Please enter your telephone number';

应该是

  if (!$_POST['telephone']) {
        $errTelephone = 'Please enter your telephone number';

答案 1 :(得分:0)

看起来你的POST HTTP Handler在你的服务器上是禁用/不工作的,如果它在你的localhost上运行,我认为你应该联系服务器管理员或在服务器控制面板上找到它来启用它