jQuery $.post function is not working async

53 views Asked by At

I have a simple jquery script on a main.php that creates message (see first $.post) and then sends notifications (see second $.post):

$.post('core/functions/user/message/message.create.php', { to_user: to_user, message: message }, function() {

    $('.messages').load('account_page_msg.php?page_id=10');
});

$.post('core/functions/user/notify_email.php', { pushTitle: pushTitle, pushMessage: message });

It triggers a notify_email.php that looks like this:

// email function
$mail = new myPHPMailer;
$mail->isSMTP();
$mail->Host = '..........';
$mail->SMTPAuth = true;
$mail->Username = '.........';
$mail->Password = '.........';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

$mail->CharSet = 'UTF-8';
$mail->addAddress('[email protected]', '');
$mail->setFrom('[email protected]', 'iHusky.com');
$mail->addReplyTo('[email protected]', '');
$mail->isHTML(true);
$mail->Subject = 'Test email notifications';

$body = 'Email notifications';

$mail->MsgHTML($body);
$mail->send();

Why is my script on a main.php waiting for the email function to complete sending email even though I don't have an actual callback? I thought it's supposed to be asynchronous.

1

There are 1 answers

2
Abdulrehman Sheikh On

Try this. It should must work

$.ajax({
      type: 'POST',
      url: 'core/functions/user/notify_email.php',
      data: { user_id: to_user, pushTitle: pushTitle, pushMessage: message },
      success: function(response){
        alert(response);
    },
  async:true
});