我的网站没有发送联系邮件,它显示错误<div class="alert_box error"><i class="fa fa-exclamation-triangle"></i><p>undefined!</p></div>
,但即使JS设置为显示消息也没有。我的代码有错误吗?你能告诉我我错了什么吗?
HTML:
<form id="contact_form2" class="contact_form var2">
<ul>
<li>
<input type="text" name="cf_name" placeholder="Nome">
</li>
<li>
<input type="text" name="cf_subject" placeholder="Assunto">
</li>
<li>
<textarea name="cf_message" placeholder="Mensagem"></textarea>
</li>
</ul>
<button class="button type1"><span><span>Enviar</span><span>Enviar</span></span></button>
</form>
PHP:
<?php
$user_email = "marketing@fontlife.com.br";
$mail = array(
"name" => htmlspecialchars($_POST['cf_name']),
"email" => htmlspecialchars($_POST['cf_email']),
"subject" => htmlspecialchars($_POST['cf_subject']),
"message" => htmlspecialchars($_POST['cf_message'])
);
function validate($arr){
return !empty($arr['name']) && strlen($arr['message']) > 20 && filter_var($arr['email'],FILTER_VALIDATE_EMAIL);
}
if(validate($mail)){
echo mail($user_email, $mail['subject'],
"Name : {$mail['name']}\n"
."E-mail : {$mail['email']}\n"
."Message : {$mail['message']}"
);
}
?>
JS:
($('#contact_form2').length){
var cf = $('#contact_form2');
cf.append('<div class="message_container2"></div>');
cf.on("submit",function(event){
var self = $(this),text;
var request = $.ajax({
url:"bat/mail.php",
type : "post",
data : self.serialize()
});
request.then(function(data){
if(data == "1"){
text = "Sua mensagem foi enviada com sucesso!";
cf.find('input:not([type="submit"]),textarea').val('');
$('.message_container2').html('<div class="alert_box success"><i class="fa fa-smile-o"></i><p>'+text+'</p></div>')
.delay(150)
.slideDown(300)
.delay(4000)
.slideUp(300,function(){
$(this).html("");
});
}
else{
if(cf.find('textarea').val().length < 20){
text = "Sua mensagem deve conter no mínimo 20 caracteres!"
}
if(cf.find('input').val() == ""){
text = "Todos os campos devem ser preenchidos!";
}
$('.message_container2').html('<div class="alert_box error"><i class="fa fa-exclamation-triangle"></i><p>'+text+'</p></div>')
.delay(150)
.slideDown(300)
.delay(4000)
.slideUp(300,function(){
$(this).html("");
});
}
},function(){
$('.message_container2').html('<div class="alert_box error"><i class="fa fa-exclamation-triangle"></i><p>Falha na conexão com o servidor!</p></div>')
.delay(150)
.slideDown(300)
.delay(4000)
.slideUp(300,function(){
$(this).html("");
});
});
event.preventDefault();
});
}
即page。
答案 0 :(得分:0)
在条件之后,在打印错误消息之前验证text
是否有值。
if(cf.find('textarea').val().length < 20){
text = "Sua mensagem deve conter no mínimo 20 caracteres!"
}
if(cf.find('input').val() == ""){
text = "Todos os campos devem ser preenchidos!";
}
if (text != undefined && text != ""){
$('.message_container2').html('<div class="alert_box error"><i class="fa fa-exclamation-triangle"></i><p>'+text+'</p></div>')
.delay(150)
.slideDown(300)
.delay(4000)
.slideUp(300,function(){
$(this).html("");
});
}