0

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('jetprofile@yahoo.com', '');
$mail->setFrom('admin@ihusky.com', 'iHusky.com');
$mail->addReplyTo('jetprofile@yahoo.com', '');
$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.

ekashking
  • 387
  • 6
  • 19
  • See https://stackoverflow.com/questions/10802156/ajax-post-async – dont_trust_me Jan 17 '19 at 05:58
  • https://stackoverflow.com/questions/9516900/how-can-i-create-an-asynchronous-function-in-javascript – Ashwin Golani Jan 17 '19 at 05:59
  • Are you asking why your *server* script isn't async? – Tomalak Jan 17 '19 at 06:35
  • Yes. I thought email function is supposed to be running on the background. Instead, if I want to repeat the script on the `main.php` when creating a new message it's stuck and waiting till the email function is complete. – ekashking Jan 17 '19 at 06:43
  • I don't see anything about how exactly you "repeat the script on the `main.php`" in your question, you would need to explain better what you do, what you expect to happen, and what happens instead. – Tomalak Jan 17 '19 at 07:45
  • Say, the script on the `main.php` is a block of code that I execute with a Send button. So it takes a message from a message field and sends it to `message.create.php` (first `$.post`). And then with a second `$.post` the notifications isis sent through the email. Now, if, right after, very quickly I type another message and click a Send button again, the system is frozen because the code is waiting for the second `$.post ` to complete sending email. I don't want it to wait for cuz it sometimes takes up to 10 sec for the email to be sent, I want to be sent on the background. – ekashking Jan 17 '19 at 07:58
  • I see a misconception in that description. You write "(first post) **and then** (second post)". There is no "and then". These two posts happen at the same time, the second post does not wait for the first one to finish. All the Ajax requests you make are perfectly asynchronous. Maybe things take a few seconds to show up in your page because the server response is slow, but your page is not "frozen" (let alone your "system", whatever that means). Take a look at the network tab in the developer tools to see when requests happen and how long they take. – Tomalak Jan 17 '19 at 08:23
  • @Tomalak Good point. Thanks – ekashking Jan 17 '19 at 08:46
  • @Tomalak so, is it possible to run a $.post request on the background without waiting for it to complete? – ekashking Jan 17 '19 at 08:50
  • Yes, and your post requests already do that, because this is the default. So *unless* you have `$.ajaxSetup({async: false});` somewhere in your code, all your Ajax requests are completely asynchronous right now. – Tomalak Jan 17 '19 at 08:53

1 Answers1

1

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
});
  • Please don't post unexplained answers. If you think the OP did something wrong, write down what and why. – Tomalak Jan 17 '19 at 06:33
  • It didn't change. I want this `$.ajax` to get triggered and be forgotten. Instead, my script got stuck till the email function on the other page is complete. – ekashking Jan 17 '19 at 06:33