无法从php / ajax表单解释400错误请求错误

时间:2016-02-14 12:55:22

标签: php jquery html ajax forms

通过我的表单提交电子邮件时,我总是得到一个无法解释的400错误,我无法理解为什么会这样。

这适用于我的本地服务器,但是当我将其上传到实时website

这是我的HTML

<form method="post" id="ajax-contact" class="clearfix" action="js/mailer.php">
    <input class="sb-search-input" placeholder="I want your freebies!" type="email" id="email" name="name" >
    <input class="sb-search-submit" type="submit">
    <button class="formbutton" type="submit">
        <span class="fa fa-check"></span>
    </button>
</form>

这是我的AJAX

$(function() {

// Get the form.
var form = $('#ajax-contact');

// Get the messages div.
var formMessages = $('#form-messages');

// Set up an event listener for the contact form.
$(form).submit(function(e) {
    // Stop the browser from submitting the form.
    e.preventDefault();

    // Serialize the form data.
    var formData = $(form).serialize();

    // Submit the form using AJAX.
    $.ajax({
        type: 'POST',
        url: $(form).attr('action'),
        data: formData
    })
    .done(function(response) {
        // Make sure that the formMessages div has the 'success' class.
        $(formMessages).removeClass('error');
        $(formMessages).addClass('success');

        // Set the message text.
        $(formMessages).text(response);

        // Clear the form.
        $('#email').val('');

    })
    .fail(function(data) {
        // Make sure that the formMessages div has the 'error' class.
        $(formMessages).removeClass('success');
        $(formMessages).addClass('error');

        // Set the message text.
        if (data.responseText !== '') {
            $(formMessages).text(data.responseText);
        } else {
            $(formMessages).text('Oops! An error occured and your message could not be sent.');
        }
    });

});

});

这是我的PHP

<?php

// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get the form fields and remove whitespace.
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);

    echo "$email";

    // Check that data was sent to the mailer.
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        // Set a 400 (bad request) response code and exit.
        http_response_code(400);
        echo "Oops! There was a problem with your submission. Please complete the form and try again.";
        exit;
    }

    // Set the recipient email address.
    $recipient = "ahamdan@kindredbay.co.uk, aemmadi@kindredbay.co.uk";

    // Set the email subject.
    $subject = "New enquiry from $email";

    // Build the email content.
    $email_content .= "Email: $email\n\n";


    // Build the email headers.
    $email_headers = "From: $email <$email>";

    // Send the email.
    if (mail($recipient, $subject, $email_content, $email_headers)) {
        // Set a 200 (okay) response code.
        http_response_code(200);
        echo "Thank You! Your message has been sent.";
    } else {
        // Set a 500 (internal server error) response code.
        http_response_code(500);
        echo "Oops! Something went wrong and we couldn't send your message.";
    }

} else {
    // Not a POST request, set a 403 (forbidden) response code.
    http_response_code(403);
    echo "There was a problem with your submission, please try again.";
}

?>

1 个答案:

答案 0 :(得分:1)

尝试在表单中替换它:

<input class="sb-search-input" placeholder="I want your freebies!" type="email" id="email" name="email" >
相关问题