简体   繁体   中英

Cannot POST /index.php

I am new to php and I'm trying to get a contact form to send to my email. Every time I click the submit button, it redirects me to a page that says 'Cannot POST /index.php'. I have tried running it from localhost and deployed it to heroku, and still get the same error. The html and php files are in the same folder.

The console states the following error: 'Failed to load resource: the server responded with a status of 404 (Not Found)'.

The HTML code for the form is

<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>

The PHP file is:

<?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>';
}
}
    }
?>

Thanks for the help.

you need to set the name for your submit button since you are checking for it in the PHP.

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

Therfore add it as follows:

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

also you need to check that the php is in the index.php that you are directing the form to send the data to.

also i think this is a typo (probably a copy / paste typo)

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

should that be

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

看起来您的 POST HTTP 处理程序已禁用/无法在您的服务器上运行,如果它在您的本地主机上运行,​​我认为您应该联系服务器管理员或在服务器控制面板上找到以启用它

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM