当我有这个时,我的PHP脚本不会发送回复:
$headers = "From: " . $clientEmail . "<" . $clientEmail . ">" . "\r\n" . "Reply-To: " . $clientEmail;
但是当我改变它时,它正在工作:
return mail($clientEmail, $subject . " (copy contact form xxx)", $message, $headers);
我有回复邮件,但echo
无效:
echo 'Message has been sent (OK)';
当我点击正确发送时,我希望看到发送消息,就像消息现在显示丢失的用户名一样。
我不想将我转到新的回复消息页面,而是希望留在表单提交页面上并对正确的帖子发表评论。
我需要在.js
和.php
中更改哪些内容?
HTML:
<form action="assets/contact.php" class="contact-form long_contact" method="POST">
<div class="form-group">
<input type="text" name="username" id="username" class="username form-control" placeholder="Your Username" value="">
<i class="fa fa-user"></i>
<span class="cross">x</span>
<span class="verify"><i class="fa fa-check" aria-hidden="true"></i></span>
<div class="alert alert-danger custom-alert">
User must be more than 3 letter
</div>
</div>
<div class="form-group">
<input type="text" name="email" class=" email form-control" placeholder="Your Email" value="">
<i class="fa fa-envelope"></i>
<span class="cross">x</span>
<span class="verify"><i class="fa fa-check" aria-hidden="true"></i></span>
<div class="alert alert-danger custom-alert">
Email can not be empty
</div>
</div>
<div class="form-group">
<input type="text" name="subject" class="subject form-control" placeholder="Your subject" value="">
<i class="fa fa-pencil"></i>
<span class="cross">x</span>
<span class="verify"><i class="fa fa-check" aria-hidden="true"></i></span>
<div class="alert alert-danger custom-alert">
Subject can not be empty
</div>
</div>
<div class="form-group">
<textarea class=" message form-control" name="message" placeholder="Your Message"></textarea>
<i class="fa fa-comments message-icon"></i>
<span class="cross">x</span>
<span class="verify"><i class="fa fa-check" aria-hidden="true"></i></span>
<div class="alert alert-danger custom-alert">
Message should be more than <b>10</b> characteres
</div>
</div>
<div class="form-group">
<input type="submit" name="submit" value="SEND" class="btn btn-success btn-block">
<i class="fa fa-paper-plane send-icon"></i>
</div>
</form>
JavaScript的:
$(function () {
'use strict';
// error variables
var UserError = true,
EmailError = true,
SubError = true,
MsgError = true;
$(".username").blur(function() {
if($(this).val().length < 4) {
$(this).css('border','1px solid #F00');
$(this).parent().find('.custom-alert').fadeIn(300).end().find('.asterix').fadeOut(300).end().find('span.cross').fadeIn(300).end().find('span.verify').fadeOut(300);
UserError = true;
} else {
$(this).css('border','1px solid #080');
$(this).parent().find('.custom-alert').fadeOut(300).end().find('.asterix').fadeOut(300).end().find('span.verify').fadeIn(300).end().find('span.cross').fadeOut(300);
UserError = false;
}
});
$(".email").blur(function() {
if($(this).val() === '') {
$(this).css('border','1px solid #F00');
$(this).parent().find('.custom-alert').fadeIn(300).end().find('.asterix').fadeOut(300).end().find('span.cross').fadeIn(300).end().find('span.verify').fadeOut(300);
EmailError = true;
} else {
$(this).css('border','1px solid #080');
$(this).parent().find('.custom-alert').fadeOut(300).end().find('.asterix').fadeOut(300).end().find('span.verify').fadeIn(300).end().find('span.cross').fadeOut(300);
EmailError = false;
}
});
$(".subject").blur(function() {
if($(this).val() === '') {
$(this).css('border','1px solid #F00');
$(this).parent().find('.custom-alert').fadeIn(300).end().find('.asterix').fadeOut(300).end().find('span.cross').fadeIn(300).end().find('span.verify').fadeOut(300);
SubError = true;
} else {
$(this).css('border','1px solid #080');
$(this).parent().find('.custom-alert').fadeOut(300).end().find('.asterix').fadeOut(300).end().find('span.verify').fadeIn(300).end().find('span.cross').fadeOut(300);
SubError = false;
}
});
$(".message").blur(function() {
if($(this).val() .length < 10) {
$(this).css('border','1px solid #F00');
$(this).parent().find('.custom-alert').fadeIn(300).end().find('.asterix').fadeOut(300).end().find('span.cross').fadeIn(300).end().find('span.verify').fadeOut(300);
MsgError = true;
} else {
$(this).css('border','1px solid #080');
$(this).parent().find('.custom-alert').fadeOut(300).end().find('.asterix').fadeOut(300).end().find('span.verify').fadeIn(300).end().find('span.cross').fadeOut(300);
MsgError = false;
}
});
// submit form
$('.contact-form').submit(function(event) {
if(UserError === true || EmailError === true || SubError === true || MsgError === true) {
event.preventDefault(); // prevent sending
$('.username,.email,.subject,.message').blur();
}
});
});
&#13;
<?php
// Email address verification
function isEmail($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
if($_POST) {
// Enter the email where you want to receive the message
$emailTo = 'xyz@gmail.com';
$username = addslashes(trim($_POST['username']));
$clientEmail = addslashes(trim($_POST['email']));
$subject = addslashes(trim($_POST['subject']));
$message = addslashes(trim($_POST['message']));
if($username != '' && isEmail($clientEmail) && $subject != '' && $message != '') {
// Send email
$message = "Message from: " . $username . "\r\n" . $message;
$headers = "From: " . $clientEmail . "<" . $clientEmail . ">" . "\r\n" . "Reply-To: " . $clientEmail;
mail($emailTo, $subject . " (contact form xxx)", $message, $headers);
return mail($clientEmail, $subject . " (copy contact form xxx)", $message, $headers);
}
echo 'Message has been sent (OK)';
}
?>
&#13;
答案 0 :(得分:0)
问题在于,您希望显示文本的echo
代码位于HTML的错误位置。它可以位于隐藏标记中,也可以位于script
内,也可以位于HTML注释中。确保它显示在正确的位置。