An email is sent when I update the status. Issue is, it has many other fields and whenever I update anyone of them, it shoots the email again. All I want to ask for confirmation if you want to send an email or no. I did use ajax to do it but in console, it says email sent but I don't get anything in inbox.
Javascript code
<script type="text/javascript">
function send_email(){
var delete_confirmed=confirm("Are you sure you want to send email?");
if (delete_confirmed==true) {
$.ajax({
type: "POST",
url: 'email.php',
data:{action:'call_this'},
success:function() {
console.log('email sent');
}
});
}}
</script>
email.php
if(isset($_POST['status']) == 'completed' && $_POST['action'] == 'call_this'){
$cmail = new PHPMailer;
$cmail->isSMTP(); // Set mailer to use SMTP
$cmail->SMTPAuth = true; // Enable SMTP authentication
$cmail->Host = $smtp_server; // Specify main and backup SMTP servers
$cmail->Username = $username; // SMTP username
$cmail->Password = $password; // SMTP password
$cmail->SMTPSecure = "ssl"; // Enable TLS encryption, `ssl` also accepted
$cmail->Port = 465; // TCP port to connect to
$cmail->setFrom("example@example.com", "Example");
$q_mail = "SELECT * FROM order_details WHERE order_id = '$_GET[edit_id]'";
$r_mail = mysqli_query($conn, $q_mail);
while ($r_rows = mysqli_fetch_assoc($r_mail)) {
$cmail->addAddress($r_rows['email'], $r_rows['firstname']); // Add a recipient
$cmail->isHTML(true); // Set email format to HTML
$bodyContent = 'test message';
$cmail->Subject = "test subject" ;
$cmail->Body = $bodyContent;
if(!$cmail->send()) {
$error = $cmail->ErrorInfo;
} else {
$error = 'Message has been sent';
}
}
}
form HTML
<div class="col-md-2" style="padding-left:0px;" ></br>
<input type="submit" class="btn navbar-bar btn-xs btn-success" name="status" onclick="send_email();" value="Update">
</div>