我正在通过PHPMailer发送电子邮件。工作正常。问题出在ajax调用中。如果我的数据返回“ null”,则对话框起作用。
我尝试了if / else if / else,并将成功消息设置为默认开关。我的控制台未显示任何反馈,也没有弹出对话框。唯一有效的是电子邮件。
前端:
<body class="text-center">
<div id="incorrect_email_dialog">
<h3>Oops! It looks like you entered an incorrect email</h3>
<h3>Give it another try</h3>
</div>
<div id="correct_email_dialog">
<h3>Please check the email associated with your account</h3>
<h3>for instructions to reset your password</h3>
</div>
<div class="container login-container">
<form id="forgot_pass_form" class="form-signin">
<img class="login_logo" src="css/images/logos/example_logo72x72.png">
<h1 class="font-weight-normal">Please enter email</h1>
<input type="email" id="inputEmail" name="inputEmail" class="form-control" placeholder="Enter email associated with account" required autofocus>
<button id="login_btn" class="btn btn-lg btn-primary btn-block" type="submit">Submit</button>
<div class="register-container">
<p><a class="register-p" href="login.php">Back to Login</a></p>
</div>
</form>
</div>
</body>
<script>
var original_error_dialog = $('#incorrect_email_dialog').html();
$('#forgot_pass_form').submit(function(e) {
e.preventDefault();
console.log(email);
console.log('Submitted form');
$.ajax({
type: 'post',
url: 'includes/token_send.php',
dataType: 'json',
data: $(this).serialize(),
success: function(data) {
console.log(data);
switch(data) {
case 'Error':
console.log('Error');
$('#incorrect_email_dialog').html('<h3>Oops. There was a problem</h3>');
$('#incorrect_email_dialog').dialog('open');
break;
case 'null':
console.log('Data was null');
$('#incorrect_email_dialog').html(original_error_dialog);
$('#incorrect_email_dialog').dialog('open');
break;
default:
$('#correct_email_dialog').dialog('open');
console.log('All good');
break;
}
}
})
});
后端:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require_once '../PHPMailer/src/PHPMailer.php';
require_once '../PHPMailer/src/SMTP.php';
include '../../config/DB.php';
if($_SERVER['REQUEST_METHOD'] === 'POST') {
$email = $_POST['inputEmail'];
try { $db = new DB(); } catch (\Exception $e) { $e->getMessage(); }
$result = $db->getRow('SELECT email FROM users WHERE email=?', [$email]);
$input_email = $result['email'];
if($input_email !== null) {
$to = $input_email;
$token = bin2hex(random_bytes(8));
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = 'localhost';
$mail->SMTPDebug = 2;
$mail->setFrom('noreply@example.com', '');
$mail->addAddress($to, '');
$mail->Subject = 'Example Password Reset Request';
$mail->isHTML(true);
$mail->Body = '
<h2 style="padding:10px;background:#ec1414;font-size:16px;color:#fff;">Example Password Reset</h2>
<br><br>
<p>Use this link to reset your password</p>
<br>
<a style="text-decoration:none!important;" href="http://example.local/reset.php?token='.$token.'&email='.$input_email.'">Click here to reset</a>';
if(!$mail->send()) {
echo json_encode('Error');
} else {
echo json_encode('Success');
}
} else {
echo json_encode('null');
}
}
我在收件箱中收到了电子邮件,但是在“已提交的表单” console.log语句之后却什么也没有。
我也尝试过:
case 'Success':
$('#correct_email_dialog').dialog('open');
console.log('All good');
break;
代替default
无济于事。
答案 0 :(得分:1)
好的,我做了一些挖掘,发现了解决方法as a comment to this question.
我忘记了我离开了
$mail->SMTPDebug = 2;
代码中的!我所做的只是
//$mail->SMTPDebug = 2;
和动臂,固定的。电子邮件发送后,我的jQuery对话框出现。一切都很好。 :)
我今天了解了有关PHPMailer调试模式以及它如何与Ajax交互的知识。朋友,感谢您为我指明了正确的方向。
这是邮件转储消失的控制台日志。
这是调用jQuery Dialog交互的ajax成功。
再次感谢您的帮助。