PHPフォームとAJAXを使用して簡単なメールを送信しようとしています。ただし、実行するたびに、コードはemail could not be sent, please try again
エラーをスローします。なぜそれが起こっているのですか?
アラート シーケンスの変数も試しましalert
たが、うまくいきませんでした。変数を確認したところ、有効に見えますが、何かを見落としているようです。
Javascript/jQuery (<head>
ブロック内)
<script type="text/javascript" src="js2/jquery-1.7.2.min.js"></script>
<script>
$("#form2").submit(function(e){
$(".error").remove();
$('#cancel2').click(function(){
return;
});
var hasError = false;
var emailReg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var nameVal = $("#name").val();
if (nameVal == '') {
$("#name").after('<span class="error">You forgot to enter the name.</span>');
hasError = true;
}
var emailToVal = $("#emailTo").val();
if (emailToVal == '') {
$("#emailTo").after('<span class="error">You forgot to enter the email address to send to.</span>');
hasError = true;
} else if(!emailReg.test(emailToVal)) {
$("#emailTo").after('<span class="error">Enter a valid email address to send to.</span>');
hasError = true;
}
var emailFromVal = $("#emailFrom").val();
if(emailFromVal == '') {
$("#emailFrom").after('<span class="error">You forgot to enter the email address to send from.</span>');
hasError = true;
} else if (!emailReg.test(emailFromVal)) {
$("#emailFrom").after('<span class="error">Enter a valid email address to send from.</span>');
hasError = true;
}
if (hasError == false){
$('.navLinks').hide();
$('.navLinks:before').hide();
$('.navLinks:after').hide();
s
$.ajax({
"type":"POST",
"url":"sendemail.php",
"data": { name: nameVal, emailTo: emailToVal, emailFrom: emailFromVal },
"dataType":'json',
"success":function(){
//$('#sumbit1').after('<img src="/img/wait.gif" alt="wait" />');
alert('Your email has been sent! For faster answer please call us at: (403) 454-5526');
$('.navLinks').hide();
},
"error": function (){
alert('Your email could not be sent, please try again.');
}
});
}
e.preventDefault();
return;
});
</script>
HTML
<form name="send-to-friend" id="form2" class="form" method="post" action="sendemail.php">
<label for="name" class="blockItem">Your Name</label>
<input type="text" id="name" class="blockItem" name="your name" maxlength="60" size="35"/>
<label for="emailFrom" class="blockItem">Your Email Address</label>
<input type="text" id="emailFrom" class="blockItem" name="your email" maxlength="60" size="35" />
<label for="emailTo" class="blockItem">Your Friend's E-mail Address</label>
<input type="text" id="emailTo" class="blockItem" name="your friends email" maxlength="60" size="35" />
<input class="button" id="submitt2" type="submit" value="SUBMIT" />
</form>
PHP
<?php
$mailTo = $_POST['emailToVal'];
$mailFrom = $_POST['emailFromVal'];
$name = $_POST['nameVal'];
$message = 'I found this company recommended for residential and office cleaning in Calgary. Their website is FineMaid "dot" com (replace "dot", of course in browser\s address bar). Cheers!';
$subject = $name . ' recommends Fine Maid';
$send = @mail($mailTo, $subject, $message, "From: ".$mailFrom);
?>