0

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>
stack kid
  • 117
  • 2
  • 12
  • 1
    show the html as well – Mudassir Hasan Sep 08 '16 at 10:39
  • CLARIFICATION PLEASE: You want to stop it sending emails when anything other than the status is changed? Right or Wrong? – RiggsFolly Sep 08 '16 at 10:40
  • Where is `$_POST['status']` sent? – Jonnix Sep 08 '16 at 10:41
  • you are not sending status data from ajax in PHP email file – Ish Sep 08 '16 at 10:41
  • Exactly...RiggsFolly... Well, `$_POST['status']` sends data to database, and then email takes data from database to send an email.. let me update that code too... – stack kid Sep 08 '16 at 10:44
  • Then you need to look at what is causing this script to be run when you change any of the other fields on your form, not stopping an incorrect execution by adding a woops message – RiggsFolly Sep 08 '16 at 12:26
  • Is it possible to have any such confirmation made in php before the script so when it comes to that script it ask for confirmation or it can happen only with javascript? – stack kid Sep 08 '16 at 12:54
  • Okay, though its not a very good way but it worked... separate the status field altogather – stack kid Sep 08 '16 at 13:17

2 Answers2

2

In your PHP you're checking for isset($_POST['status']) == 'completed', which will always be false. isset will only return a boolean. Also, it will always be false as you're not passing in the status to email.php, only the action. You need to make a couple of changes to fix this. The first is to your javascript:

$.ajax({
   type: "POST",
   url: 'email.php',
   data:{action:'call_this', status: 'completed'},
   success:function() {
       console.log('email sent');
   } 

});

The second is to the PHP script:

if( ( isset($_POST['status']) && $_POST['status'] == 'completed' ) && $_POST['action'] == 'call_this'){
Styphon
  • 10,304
  • 9
  • 52
  • 86
  • I have three more statuses which when I select send emails. Currently, made the changes you said and its not working... It says email sent but nothing received. :( – stack kid Sep 08 '16 at 11:55
0

The fact that you are receiving some sort of feedback from the ajax request suggests the problems lie elsewhere. The line below was incorrect - you were missing quotes from the edit_id - and when accessing $_GET / $_POST variables within a string I always surround with curly braces as below.

$q_mail = "SELECT * FROM `order_details` WHERE `order_id` = '{$_GET['edit_id']}';";

It could just be that this was misstyped when you posted the question but maybe not - check the error logs whenever things do not happen as expected.

And if you look at this thread there is a good example of using try/catch to deduce where things are going wrong.

Community
  • 1
  • 1
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46