I am building a user logon system on my website. My code works fine with no problems. See the the reset password Ajax request.
html
JavaScript
x
<div id="forgotPass"></div>
<script type="text/javascript">
$(document).ready(function(){
$("#forgotBtn").click(function(event){
if ($("#forgotForm")[0].checkValidity()){
event.preventDefault();
$("#forgotBtn").val('Validating...');
$.ajax({
url: 'assets/php/action.php',
method: 'post',
data: $('#forgotForm').serialize()+'&action=forgot',
success:function(response){
//console.log(response);
$("#forgotBtn").val('Reset Password');
$("#forgotForm")[0].reset();
$('#forgotPass').html(response);
}
});
}
});
});
</script>
PHP
JavaScript
//Error or Success Message Alert
public function messageShow($type, $message){
return '<div class="alert alert-'.$type. ' alert-dismissible">
<button type="button" class="close"
data-dismiss="alert">×</button>
<strong class="text-center">'.$message.'</strong>
</div>';
}
// FORGOT PASSWORD AJAX HANDLE =========================================
if (isset($_POST['action']) && $_POST['action'] == 'forgot'){
$email = $user->test_input($_POST['email']);
$user_found = $user->currentUser($email);
if ($user_found != null) {
$token = uniqid();
$token = str_shuffle($token);
$user->forgot_password($token,$email);
//Server settings
try {
// Eable verbose debug output
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
// Send using SMTP
$mail->isSMTP();
//Set the SMTP server to send through
$mail->Host = 'smtp.gmail.com';
//Enable SMTP authentication
$mail->SMTPAuth = true;
//SMTP username
$mail->Username = Database::USERNAME;
// SMTP password
$mail->Password = Database::PASSWORD;
//Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
// TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
$mail->Port = 587;
//Local Code from from Ajax Request & public class
$mail->setFrom(Database::USERNAME, 'CShare Support');
$mail->addAddress($email);
$mail->isHTML(true);
$mail->Subject = 'Reset Password';
$mail->Body = '<h3>Click the below link to reset your password. Attention: This link will expire in 10 minutes!<br><a href="http://localhost/crypt.v4/reset-pass.php?email='.$email. '&token='.$token.'"><br><br><h1 class="text-center font-weignt-bold text-primary">RESET PASSWORD</h1></a><br><br><br>Regards<br><br>CShare Support.</h3>';
$mail->send();
echo $user->messageShow('success', 'We have send you password reset link, Please check you email!');
}
catch(Exception $e){
echo $user->messageShow('danger', 'Something went wrong please try again later!');
}
}
else{
echo $user->messageShow('info', 'This email is not registered!');
}
}
The problem is after a successful reset password. The div show my #forgotPass response plus the entire html structure. Why is that and how to stop it. Thanks. See the below html structure displayed in my div
JavaScript
2021-01-26 15:57:02 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP bk2sm10065994ejb.98 - gsmtp
2021-01-26 15:57:02 CLIENT -> SERVER: EHLO localhost
2021-01-26 15:57:03 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [41.60.79.136]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2021-01-26 15:57:03 CLIENT -> SERVER: STARTTLS
2021-01-26 15:57:03 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2021-01-26 15:57:03 CLIENT -> SERVER: EHLO localhost
2021-01-26 15:57:03 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [41.60.79.136]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2021-01-26 15:57:03 CLIENT -> SERVER: AUTH LOGIN
2021-01-26 15:57:04 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2021-01-26 15:57:04 CLIENT -> SERVER: [credentials hidden]
2021-01-26 15:57:04 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2021-01-26 15:57:04 CLIENT -> SERVER: [credentials hidden]
2021-01-26 15:57:04 SERVER -> CLIENT: 235 2.7.0 Accepted
2021-01-26 15:57:04 CLIENT -> SERVER: MAIL FROM:<arnoxmariaz@gmail.com>
2021-01-26 15:57:04 SERVER -> CLIENT: 250 2.1.0 OK bk2sm10065994ejb.98 - gsmtp
2021-01-26 15:57:04 CLIENT -> SERVER: RCPT TO:<ali820@outlook.com>
2021-01-26 15:57:05 SERVER -> CLIENT: 250 2.1.5 OK bk2sm10065994ejb.98 - gsmtp
2021-01-26 15:57:05 CLIENT -> SERVER: DATA
2021-01-26 15:57:05 SERVER -> CLIENT: 354 Go ahead bk2sm10065994ejb.98 - gsmtp
2021-01-26 15:57:05 CLIENT -> SERVER: Date: Tue, 26 Jan 2021 16:57:02 +0100
2021-01-26 15:57:05 CLIENT -> SERVER: To: ali820@outlook.com
2021-01-26 15:57:05 CLIENT -> SERVER: From: CShare Support <arnoxmariaz@gmail.com>
2021-01-26 15:57:05 CLIENT -> SERVER: Subject: Reset Password
2021-01-26 15:57:05 CLIENT -> SERVER: Message-ID: <edsX5pKq7hoxltVhdTOMZiuWIfyivvbs3ISNHmvFP30@localhost>
2021-01-26 15:57:05 CLIENT -> SERVER: X-Mailer: PHPMailer 6.2.0 (https://github.com/PHPMailer/PHPMailer)
2021-01-26 15:57:05 CLIENT -> SERVER: MIME-Version: 1.0
2021-01-26 15:57:05 CLIENT -> SERVER: Content-Type: text/html; charset=iso-8859-1
2021-01-26 15:57:05 CLIENT -> SERVER:
2021-01-26 15:57:05 CLIENT -> SERVER: <h3>Click the below link to reset your password. Attention: This link will expire in 10 minutes!<br><a href="http://localhost/crypt.v4/reset-pass.php?email=alio820@outlook.com&token=b03333e6c1601"><br><br><h1 class="text-center font-weignt-bold text-primary">RESET PASSWORD</h1></a><br><br><br>Regards<br><br>CShare Support.</h3>
2021-01-26 15:57:05 CLIENT -> SERVER:
2021-01-26 15:57:05 CLIENT -> SERVER: .
2021-01-26 15:57:06 SERVER -> CLIENT: 250 2.0.0 OK 1611676628 bk2sm10065994ejb.98 - gsmtp
2021-01-26 15:57:06 CLIENT -> SERVER: QUIT
2021-01-26 15:57:06 SERVER -> CLIENT: 221 2.0.0 closing connection bk2sm10065994ejb.98 - gsmtp
ÕWe have send you password reset link, Please check you email!
I do not all this to be displayed in my div. But only the massageShow = ‘We have send you password reset link’, ‘Please check you email!’
Advertisement
Answer
It seems as though the SMTP debugging is being immediately output. If you comment out the line $mail->SMTPDebug = SMTP::DEBUG_SERVER;, After commenting out the line; Case has been resolved.
Credits to El_Vanja