我正在尝试使用ajax提交表单但它不起作用。问题是它实际上告诉我它发送电子邮件,但是当我检查我的电子邮件时我什么也没得到。当我调用Ajax方法时,它似乎停止工作。这是代码:
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:
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
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;
}
}
?>
答案 0 :(得分:1)
PHP代码正在寻找以下参数:
但你有以下ajax调用:
data: "prenom=" + prenom + "&nom=" + nom + "&email=" + email + "&message=" + message
此外,最好使用一个对象,以便jQuery为您编码:
data: {
prenom_contact: prenom,
nom_contact: nom,
email_contact: email,
message_contact: message
},