0

Using Ajax to call a phpmail routine that sends out a series of emails (3 in this test) in a foreach loop when the SUBMIT button is clicked. Using the Ajax in ASYNC:FALSE mode, causes a several second delay (maybe 3 or 4 sec) until the mail routine completes, sends an "OK Sent" message back, then goes off to the new page.

WANT TO AVOID NOTICEABLE DELAY TO USER AND RUN MAILER IN BACKGROUND

Thinking to avoid the noticeable delay and have the mailer routine execute in the background, I use ASYNC:TRUE mode. Upon clicking SUBMIT, it just jumps to the header page but no email is sent out.

I was hoping that the phpmail routine would execute in the background, but it obviously gets destroyed once the page jumps to the header location.

Without the header, the phpmail routine works both in SYNC and ASYNC mode.

TRUE INTENT

What I would like to achieve is when a person publishes a document on the page, clicks the SUBMIT button, then subscribers immediately get notified via email that a new document has been published, and brings user to the next page - WITHOUT the user having to wait. Expected email list would be in the hundreds.

Using Apache on local machine and BLUEHOST as the real server.

What methods are there to achieving a trigger in code by a button press, pass an ID parameter, and have the phpmailer routine receive the data and process it in the background, while the user goes onto other parts of the site without having to wait? Solution without special add-ons would be nice...but if there is no other way, OK!

My code below if useful:

$(document).ready(function(){   
   //PUBLISH button clicked, so send email to subscribers of new questions
    $(document).on('click', '#submit', function(){
        var question_id = '<?php echo $question_id; ?>';
        console.log('question_id is: '+question_id);
        $.ajax({
            type: "POST",
            url: '/modules/posts/TEST_questions_new_send_mail.php',
            async : false,//true does not execute the email loop, just jumps to the header below
            data: { "action":'new_question_subscribers',
                    "question_id":question_id},
            success: function(data) {
                    console.log(data);                   
            },
            error: function() {
                alert('Ajax Failed');
            }
        });
        window.location.assign('https://q.localhost/modules/posts/questions.php');
    }); 
});
HDer
  • 385
  • 5
  • 17
  • Sounds like your `
    ` is submitting normally. I'll link in a couple of posts that should help
    – Phil Apr 08 '20 at 23:37
  • ***This question already has answers here: *** Not on topic at all. Not really about submitting the form, but rather getting phpmail routine to run in the background by triggering on the SUBMIT button - not associated with a form. Whether there is a form or not is a non-ssue. CAN YOU RELEASE THIS QUESTION AGAIN? – HDer Apr 09 '20 at 00:01
  • I think you misunderstand. Assuming you have `` or ` – Phil Apr 09 '20 at 02:02
  • If that is not the case, please include your HTML in your question and I'll re-open it. – Phil Apr 09 '20 at 02:03
  • You should also move your `window.location...` line into the `success` handler. Navigating away from a page while an AJAX request is in flight will abort that request – Phil Apr 09 '20 at 02:05

0 Answers0