如何显示/隐藏 PHP 文件中的 html 消息
How do I display/hide an html message from a PHP file
目前,在我的网站上,联系表格是通过一段JS验证的(我知道这是有风险的,我现在已经转到PHPMailer)。
目前触发我的联系表单下方成功或错误消息的是javascript的这一部分:
if (isValidEmail(email) && (message.length > 1) && (name.length > 1)) {
$.ajax({
type: "POST",
url: "sendphpmailer.php",
data: dataString,
success: function() {
$('button[name="submit"]').hide();
$('.error').hide()
$('.success').fadeIn(1000);
}
});
} else {
$('.error').fadeIn(1000);
}
return false;
});
});
现在我正在使用 PHPMailer,我希望我的联系表单下面的成功/错误消息是从 PHP 文件驱动的(不再是 JS 文件,因为它有时会返回成功消息,即使消息没有发送),所以我尝试过这个(没有成功)。问题出在哪里?
$mail->Subject = "New message from " . $_POST['name'];
$mail->Body = $_POST['message'];
if(!$mail->send())
{
$('button[name="submit"]').hide();
$('.error').hide()
$('.success').fadeIn(1000);}
else
{
$('.error').fadeIn(1000);
}
如果有帮助,这里是原始的完整PHP代码:
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->CharSet = 'utf-8';
//Enable SMTP debugging.
$mail->SMTPDebug = false;
//Set PHPMailer to use SMTP.
$mail->isSMTP();
//Set SMTP host name
$mail->Host = "smtp.elasticemail.com";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;
//Provide username and password
$mail->Username = "web@dshsjjh.com";
$mail->Password = "***";
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "tls";
//Set TCP port to connect to
$mail->Port = 2525;
$mail->From = $_POST['email'];
$mail->FromName = $_POST['name'];
$mail->addAddress("xyz@gmail.com");
//CC and BCC
$mail->addCC("xyz@outlook.com");
$mail->addBCC("");
$mail->isHTML(true);
$mail->Subject = "New message from " . $_POST['name'];
$mail->Body = $_POST['message'];
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}
?>
和 HTML:
<section class="contact clearfix" id="contact">
<div class="contact-container clearfix">
<form class="clearfix" id="contactfrm" method="post" name="contactfrm">
<div class="tagbox wow fadeInRightBig"><i class="fa fa-pencil"></i> Formulaire de contact
</div>
<input class="_input-1 floatlabel" id="name" name="name" placeholder="Votre nom et prénom" type="text">
<input class="_input-2 floatlabel" id="email" name="email" placeholder="Votre adresse e-mail" type="email">
<textarea class="floatlabel" id="message" name="message" placeholder="Votre message et votre numéro de téléphone"></textarea>
<button class="wow fadeInUp" name="submit" type="submit" value="Envoyer"><i class="fa fa-sign-in"></i> ENVOYER </button>
</form>
</div>
</section>
<footer class="clearfix">
<div class="success" style="display:none">
Votre message a bien été envoyé! Merci
</div>
<div class="error" style="display:none;">
E-mail non valide et/ou message vide
</div>
</footer>
'如果你使用 ajax 使用 PHPMailer 发送和发送电子邮件,那么你无法在 JavaScript 文件中检查 $mail->send()
您需要做的是以下内容在 PHP 文件中
返回 json_encode(['成功' => $mail->send()]);
在你的 Ajax 成功函数中这样做
success: function(response) {
if(response.success)
{
//success message
}
else
{
//error message
}
}
Ajax 调用:
$.ajax({
type: "POST",
url: "sendphpmailer.php",
data: dataString,
dataType:'json',
success: function(response) {
if(response.status){
/* success */
$('button[name="submit"]').hide();
$('.error').hide()
$('.success').html(response.message).fadeIn(1000);
}else{
/* error occurred show to user */
$('.success').hide();
$('button[name="submit"]').show();
$('.error').html(response.message).fadeIn(1000);
}
}
});
PHP响应:
$response = array();
if(!$mail->send()) {
$response = array('message'=>"Mailer Error: " . $mail->ErrorInfo, 'status'=> 0);
} else {
$response = array('message'=>"Message has been sent successfully", 'status'=> 1);
}
/* send content type header */
header('Content-Type: application/json');
/* send response as json */
echo json_encode($response);
相关文章:
- 如何在php文件中获取$.post-ajax传递的值
- Javascript运行php文件,然后下载文件
- 通过Ajax将JavaScript函数传递给PHP文件
- 我想使用模态通过php文件发送邮件,并且我希望在提交关闭后关闭pop
- 通过ajax到php文件获取单选按钮值
- 每天用javascript包含不同的php文件
- 如何使用javascript调用Php文件
- 如何使用php文件中的GET来获取我在.js文件中声明的变量
- 在CodeIgniter视图中将Javascript或jQuery代码作为PHP文件编写可以吗
- CORS:否'访问控制允许来源'header-但是php设置头文件
- 用PHP发送html表单和文件附件
- 动态地包含来自外部文件PHP的JavaScript
- 向文件 php 表单提交添加多个输入
- 将web服务响应转换为PDF文件- PHP, Javascript
- 基于XML文件(PHP, Jquery)的页面生成
- 如何使用jQuery.ajax发送请求到文件php
- 显示数据下拉列表从另一个文件(php,mysql,javascript)
- 两个电子邮件在同一个文件php
- 从mysql中存储和检索用户js文件.php
- 组织3个文件:PHP、HTML、JS和Smarty