从联系表单发送电子邮件时加载.php页面(电子邮件发送正常)

时间:2017-01-05 19:33:51

标签: php forms email send contact-form

我还不是很擅长php(还),当我点击发送时,我的联系表单似乎无法正常工作:我的.php页面出现了应该出现在联系表单顶部的消息(在div中称为&#34; form-letyouknow&#34;)。 我之前使用此代码制作了联系表单,使用了不同的ID,因此我确信这是一个非常简单和愚蠢的错误,但我已经多次使用它了,因为我需要有新眼睛看的人。< / p>

这是我的代码:

        <div id="form-letyouknow">
      <div id="form-messages"></div>


      <form class="form" id="contact-form" method="post" action="send.php">


        <div class="margin-bottom">
            <label id="namelab" for="name">Nom:</br></label>
            <input type="text" id="name" name="name" size="40" required>
        </div>

        <div class="margin-bottom">
            <label for="email">Adresse e-mail:<br></label>
            <input type="email" id="mail" name="email" size="40" required>
        </div>

        <div class="button" id="submit">
            <label for="message" id="message-label">Message:<br></label>
            <textarea id="msg" name="message" rows="8" cols="50" required></textarea>
        </div>

        <div class="field">
            <button type="submit" id="send">Envoyer</button>
        </div>
      </form>
    </div>

Jquery的

    $(function() {

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

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

// 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.
        $('#name').val('');
        $('#email').val('');
        $('#message').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
    // POST requests.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the form fields and remove whitespace.
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);

// Check that data was sent to the mailer.
if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
    // Set a 400 (bad request) response code and exit.
    http_response_code(400);
    echo "Oops! Il y a eu un problème. Avez-vous bien remplit tous les champs ?";
    exit;
}

// Set the recipient email address.
// FIXME: Update this to your desired email address.
$recipient = "my-email@email.com";

// Set the email subject.
$subject = "New message via website from $name";

// Build the email content.
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Message:\n$message\n";


// Send the email.
if (mail($recipient, $subject, $email_content)) {
    // Set a 200 (okay) response code.
    http_response_code(200);
    echo "Merci ! Votre message a été envoyé. J'y répondrai dans les plus brefs délais.";
} else {
    // Set a 500 (internal server error) response code.
    http_response_code(500);
    echo "Oops! Une erreur est survenue. Vous pouvez m'envoyer un e-mail à l'adresse suivante: my-email@email.com";
}

} else {
// Not a POST request, set a 403 (forbidden) response code.
http_response_code(403);
echo "Une erreur est survenue lors de l'envoi de votre message. Réessayez ou envoyez moi un e-mail à l'adresse suivante: my-email@email.com";
}


?>

感谢您抽出宝贵的时间,对不起,如果这是一个愚蠢的错误。

0 个答案:

没有答案