我使用带有ajax验证的简单HTML表单,并使用php发送邮件,但是它一直给我错误消息未发送。如果我禁用ajax,表单将会通过。我该怎么做才能解决这个问题?
HTML表单:
<form method="POST" action="/tj/send.php">
<p>
<label for="name">Name <span class="required">*</span></label>
<input type="text" name="name" id="name">
</p>
<p>
<label for="email">Email <span class="required">*</span></label>
<input type="text" name="email" id="email">
</p>
<p>
<label for="subject">Subject</label>
<input type="text" name="subject" id="subject">
</p>
<p>
<label for="subject">Message <span class="required">*</span></label>
<textarea name="message" id="message" cols="45" rows="10"></textarea>
</p>
<div class="fBtn">
<button type="submit" name="submit" id="submit" class="regButton"><i class="icon-paper-plane"></i>Send Message</button>
</div>
</form>
<div id="success"><h2>Your message has been sent. Thank you!</h2></div>
<div id="error"><h2>Sorry your message can not be sent.</h2></div>
</div>
AJAX验证:
$('#submit').click(function(){
$('input#name').removeClass("errorForm");
$('textarea#message').removeClass("errorForm");
$('input#email').removeClass("errorForm");
var error = false;
var name = $('input#name').val();
if(name == "" || name == " ") {
error = true;
$('input#name').addClass("errorForm");
}
var msg = $('textarea#message').val();
if(msg == "" || msg == " ") {
error = true;
$('textarea#message').addClass("errorForm");
}
var email_compare = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i;
var email = $('input#email').val();
if (email == "" || email == " ") {
$('input#email').addClass("errorForm");
error = true;
}else if (!email_compare.test(email)) {
$('input#email').addClass("errorForm");
error = true;
}
if(error == true) {
return false;
}
var data_string = $('.contactForm form').serialize();
$.ajax({
type: "POST",
url: $('.contactForm form').attr('action'),
data: data_string,
success: function(message) {
console.log("AJAX Error:" + message);
if(message == 'SENDING'){
$('#success').fadeIn('slow');
}
else{
$('#error').fadeIn('slow');
}
}
});
return false;
});
PHP:
<?php
/* Code by David McKeown - craftedbydavid.com */
/* Editable entries are bellow */
$send_to = "pthornt1@gmail.com";
$send_subject = "trishajohnson.net form";
/*Be careful when editing below this line */
$name = cleanupentries($_POST["name"]);
$email = cleanupentries($_POST["email"]);
$subject = cleanupentries($_POST["subject"]);
$message = cleanupentries($_POST["message"]);
$from_ip = $_SERVER['REMOTE_ADDR'];
$from_browser = $_SERVER['HTTP_USER_AGENT'];
function cleanupentries($entry) {
$entry = trim($entry);
$entry = stripslashes($entry);
$entry = htmlspecialchars($entry);
return $entry;
}
$message = "This email was submitted on " . date('m-d-Y') .
"\n\nName: " . $name .
"\n\nE-Mail: " . $email .
"\n\nMessage: \n" . $message .
"\n\n\nTechnical Details:\n" . $from_ip . "\n" . $from_browser;
$send_subject .= " - {$name}";
$headers = "From: " . $email . "\r\n" .
"Reply-To: " . $email . "\r\n" .
"X-Mailer: PHP/" . phpversion();
/* Send the message using mail() function */
mail($send_to,$send_subject,$message);
?>
答案 0 :(得分:0)
听起来像是两个不同的问题。表单提交和实际发送的电子邮件。
我可以说php无法发送电子邮件。 (对不起castis在我提交答案之前我没有看到你的评论。)
那里应该有更多代码,可能使用php mail
命令(带参数),或者为了更好的机会让收件人实际收到电子邮件,使用SMTP邮件,通常由服务器管理员设置。
另外,尝试在行if (message == 'SENDING')
上方的console.log(消息)。
此外,代码的哪一部分是任何值,字符串&#39; SENDING&#39;?由于这个原因,您的if
声明可能只是评估为假。
正如您在评论中提到的,第111行的console.log输出为AJAX Error: script.js:111
这表明消息是空的,没有。因此,你的if语句
if(message ==&#39; SENDING&#39;)确实会评估为false。
我会尝试修改你的代码,以便有2个回调,成功和错误。
success: function(data) {
$('#success').fadeIn('slow');
},
error: function(xhr) {
$('#error').fadeIn('slow');
}
有关详细信息,我发现这很有用:http://hayageek.com/jquery-ajax-post/