I have a PHPMailer form and I am using .ajax using this SO answer jquery-ajax-post-example-with-php
It is successfully sending or not sending, so I know the Mailer and captcha are working. Now, if I could get different behaviors for success and fail, that would be great, but I am not getting this right somehow.
PREFERRED BEHAVIORS
SUCCESS --> reload screen and show bootstrap modal with success msg
FAIL --> reload screen and show bootstrap modal with fail msg
I have a lot of code so, Think of the modals as vanilla as possible, and the PHPMailer is, in fact, sending as well. The problem I am having should be in the code below, but if you have to have something else, just ask. Trying to keep the question as decluttered as possible
<script type="text/javascript">
$(document).ready(function() {
var request;
$("#contactForm").submit(function(event){
event.preventDefault();
// Abort any pending request
if (request) {
request.abort();
}
// setup some local variables
var $form = $(this);
// Let's select and cache all the fields
var $inputs = $form.find("input, select, button, textarea");
// Serialize the data in the form
var serializedData = $form.serialize();
// Let's disable the inputs for the duration of the Ajax request.
// Note: we disable elements AFTER the form data has been serialized.
// Disabled form elements will not be serialized.
$inputs.prop("disabled", true);
request = $.ajax({
url: "processLogin.php",
type: "post",
data: serializedData
});
// Callback handler that will be called on success
request.done(function (response, textStatus, jqXHR){
if (response == true ) {
top.location.reload();
// top.location.href="/";
// $('#successEmail').modal('show');
} else {
// top.location.reload();
$('#failEmail').modal('show');
}
});
// Callback handler that will be called on failure
request.fail(function (jqXHR, textStatus, errorThrown){
// Log the error to the console
// console.error(
// "The following error occurred: "+
// textStatus, errorThrown
// );
// top.location.reload();
});
// Callback handler that will be called regardless
// if the request failed or succeeded
request.always(function () {
// Reenable the inputs
$inputs.prop("disabled", false);
});
});
});
</script>
PHPMailer Form
<?php
session_start();
?>
<?php
/**
* This example shows how to handle a simple contact form.
*/
$msg = '';
use PHPMailer\PHPMailer\PHPMailer;
require './mail/PHPMailer.php';
require './mail/Exception.php';
require './mail/SMTP.php';
require './mail/PHPMailerAutoload.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/securimage/securimage.php';
$securimage = new Securimage();
//Don't run this unless we're handling a form submission
if (array_key_exists('email', $_POST)) {
date_default_timezone_set('Etc/UTC');
//Create a new PHPMailer instance
$mail = new PHPMailer;
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'smtp.live.com';
$mail->SMTPAuth = true;
$mail->Username = 'email@outlook.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('email@outlook.com', 'Mailer');
$mail->addAddress('email@outlook.com', 'First Last');
$email = isset($_POST['email']) ? $_POST['email'] : null;
$name = isset($_POST['name']) ? $_POST['name'] : null;
$phone = isset($_POST['phone']) ? $_POST['phone'] : null;
$message = isset($_POST['message']) ? $_POST['message'] : null;
if ($mail->addReplyTo($_POST['email'], $_POST['name'])) {
$mail->Subject = 'Contact request';
$mail->isHTML(true);
$mail->Body = <<<EOT
<div style="width:100%">
<div><label style="color: #044F69; font-weight:bold">Email:</label> <span>{$_POST['email']}</span></div>
<div><label style="color: #044F69; font-weight:bold">Name:</label> <span>{$_POST['name']}</span></div>
<div><label style="color: #044F69; font-weight:bold">Phone:</label> <span>{$_POST['phone']}</span></div>
<div><label style="color: #044F69; font-weight:bold">Message:</label> <span>{$_POST['message']}</span></div>
</div>
EOT;
if ($securimage->check($_POST['captcha_code']) == false) {
// echo "<meta http-equiv='refresh' content='0'>";
exit;
}
//Send the message, check for errors
if (!$mail->send()) {
$msg = 'Sorry, something went wrong. Please try again later.';
} else {
header("Location: /");
exit;
}
} else {
$msg = 'Invalid email address, message ignored.';
}
}
?>
P.S. The code as I have it (See above), shows the fail modal and no reload regardless of whether it passes or fail.
Thanks in advance!