简体   繁体   English

带有ajax的表单不会发送电子邮件

[英]Form with ajax doesn't send the email

I'm trying to submit a form with ajax and it just doesn't work. 我正在尝试使用ajax提交表单,但它不起作用。 The problem is that it actually tell me that it send the email, but when I check my email I got nothing. 问题是它实际上告诉我它发送了电子邮件,但是当我检查电子邮件时却什么也没有。 It seems to stop working when I call the Ajax method. 当我调用Ajax方法时,它似乎停止工作。 Here is the code: 这是代码:

HTML: HTML:

<form method="POST" id="form_contact" class="col_50">
    <input type="text" id="prenom_contact" name="prenom_contact" placeholder="Prénom" />
    <input type="text" id="nom_contact" name="nom_contact" placeholder="Nom" />
    <input type="text" id="email_contact" name="email_contact" placeholder="Courriel" />
    <textarea id="message_contact" name="message_contact" rows="6" placeholder="Votre message..."></textarea>

    <input type="submit" id="submit_contact" name="submit_contact" value="Envoyer" />
</form>

JS: JS:

var prenom = $("#prenom_contact").val(),
    nom = $("#nom_contact").val(),
    email = $("#email_contact").val(),
    message = $("#message_contact").val();

var error_count = 0;

//THERE IS ADDITIONNAL CODE HERE WHO'S JUST CHECHKING IF THE VAR AREN'T EMPTY, I'M JUST NOT SHOWING IT BECAUSE I KNOW THAT IT IS WORKING

//IF THE FORM HAS NO ERROR
if(error_count === 0)
{
    $.ajax({
        type: "POST",
        url: "http://davidstpierre.ca/new/form_contact.php",
            data: "prenom=" + prenom + "&nom=" + nom + "&email=" + email + "&message=" + message,               
        error: function() {
            alert("Une erreur est survenue. Veuillez actualiser la page de nouveau et réessayer.");
        },
        success: function() {
            //RESET THE FORM
            $('#prenom_contact').val('');
            $('#nom_contact').val('');
            $('#email_contact').val('');
            $('#message_contact').val('');

            alert("Good job!");
        }
    });
}

PHP: PHP:

<?php
if($_POST)
{   
    echo "<p id='contact_success'>Succès!</p>";

    $prenom = $_POST['prenom_contact'];
    $nom = $_POST['nom_contact'];
    $email = $_POST['email_contact'];   
    $message = $_POST['message_contact'];       

    $destinataire = "saintpierre.david@gmail.com";
    $titre_courriel = "Un nouveau message de la part de " .$prenom." ".$nom. " sur votre portfolio.";

    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= 'From: '.$prenom.' '. $nom .' <'.$email.'>' . "\r\n";

    $codehtml=
    "<html><body>" .
        "<p><strong>Nom:</strong> ".htmlspecialchars($nom)."</p>".
        "<p><strong>Prénom:</strong> ".htmlspecialchars($prenom)."</p>".
        "<p><strong>Adresse électronique:</strong> ".htmlspecialchars($email)."</p>".
        "<p><strong>Commentaires:</strong> ".htmlspecialchars($message)."</p><br>" .
    "</body></html>";

    if(!empty($nom) & !empty($prenom) & !empty($email) & !empty($message))
    {
        if(mail($destinataire, $titre_courriel, $codehtml, $headers))
        {
            return true;
        }
        else {
            return false;
        }
    }
    else {
        return false;
    }
}
?>

The PHP code is looking for the following parameters: PHP代码正在寻找以下参数:

  1. prenom_contact prenom_contact
  2. nom_contact nom_contact
  3. email_contact email_contact
  4. message_contact message_contact

But you have the following for the ajax call: 但是您对ajax调用具有以下要求:

data: "prenom=" + prenom + "&nom=" + nom + "&email=" + email + "&message=" + message

Also, it is best to use an object so jQuery will encode for you: 另外,最好使用一个对象,以便jQuery为您编码:

data: {
    prenom_contact: prenom,
    nom_contact: nom,
    email_contact: email,
    message_contact: message
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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