我知道这个问题已经被问过无数次了,但我一直找不到适合我的答案。
我的联系表格似乎验证得很好,当输入合适的数据并单击“发送消息”按钮时,会显示一条消息,说明该消息实际上已发送。屏幕上没有错误消息。
我的 html 代码如下所示:
<form action="send.php" id="contact_form" method="post" name="contact_form">
<ul>
<li>
<label>NAME <span class="required">*</span></label>
<input type="text" name="name" id="name" value="" class="requiredField" />
</li>
<li>
<label>EMAIL <span class="required">*</span></label>
<input type="text" name="email" id="email" value="" class="requiredField email" />
</li>
<li class="textarea">
<label>MESSAGE <span class="required">*</span></label>
<textarea name="message" id="message" rows="20" cols="30" class="requiredField"></textarea>
</li>
<li class="button_form">
<input name="submitted" id="submitted" value="Send Message" class="submit" type="submit" />
<input id="reset" type="reset" value="Reset Form" class="submit" />
</li>
</ul>
</form>
<script type="text/javascript">
//<![CDATA[
$(document).ready(function(){
$("#form").validate();
});
//]]>
我的 JS 看起来像这样:
$(document).ready(function() {
$('form#contact_form').submit(function() {
$('form#contact_form .error').remove();
var hasError = false;
$('.requiredField').each(function() {
if(jQuery.trim($(this).val()) == '') {
var labelText = $(this).prev('label').text();
$(this).parent().append('<span class="error">Please enter your '+labelText+'</span>');
$(this).addClass('inputError');
hasError = true;
}
else if($(this).hasClass('email')) {
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
if(!emailReg.test(jQuery.trim($(this).val()))) {
var labelText = $(this).prev('label').text();
$(this).parent().append('<span class="error">Please a valid '+labelText+'</span>');
$(this).addClass('inputError');
hasError = true;
}
}
});
if(!hasError) {
$('form#contact_form input.submit').fadeOut('normal', function() {
$(this).parent().append('');
});
var formInput = $(this).serialize();
$.post($(this).attr('action'),formInput, function(data){
$('form#contact_form').slideUp("fast", function() {
$(this).before('<p class="success">Thank you!<br/>Your email was sent successfully.<br/>I will contact you as soon as possible.</p>');
});
});
}
return false;
});
});
我的 PHP 看起来像这样:
<?php
error_reporting(E_ALL ^ E_NOTICE); // hide all basic notices from PHP
if(!isset($hasError)) {
$emailTo = 'name@domain.com';
$subject = 'Submitted message from '.$name;
$sendCopy = trim($_POST['sendCopy']);
$body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
$headers = 'From: ' .' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
我在代码中遗漏了什么吗?任何帮助将非常感激!