Send ajax requests one by one

Asked

Viewed 27 times

0

I’m sending an email to a list of say 100 people, process this sending via Ajax, the normal is the ajax send to a page and in Success or error of it, it gives me the return what occurred, until then blz, I’m already doing with Ajax and PHP, however i want to see if it is possible that the return is one by one as the while of php is processing the upload, example:

Iniciando envio 1....
Email 1 enviado com sucesso
Iniciando envio 2...
Email 2 enviado com sucesso
Iniciando envio 3...
Email 3 enviado com sucesso
Iniciando envio 4...
Erro no envio do Email 4

And so on until the list is finished, today it returns me all that message quoted above at once, only after finishing while all of php. Below the ajax code I’m using

$('#ideMarketing').change(function () {
    var ideMarketing = $('#ideMarketing').val();
    if (ideMarketing == "") {
        $('#verificandoEmail').html("");
        $("#resultVerificandoEmail").html("");
        $('#assunto').val("");
        $('#htmlMensagem').val("")

    } else {
        $.ajax
            ({
                type: 'POST',
                dataType: 'json',
                url: 'pagina_processamento.php',
                beforeSend: function () {
                    $("#resultVerificandoEmail").html("");
                    $('#verificandoEmail').html("<img src='img/load.gif' id='load'>");
                },
                data: {
                    ideMarketing: ideMarketing
                },
                success: function (msg) {
                    $('#resultVerificandoEmail').html(msg);                
                }
            });
    }
})

1 answer

-1

You can make your JS go through a list of emails or contacts and then call its function.

Ex1 foreach

var emails = ["[email protected]", "[email protected]", "[email protected]"];
fruits.forEach(sua_função_de_emails(email));

A second approach would be to let your back-end do this sending, so in your PHP language you can scroll through your list and fire your emails.

Another approach would be for you to integrate your application with a bulk email sending service, like Sendgrid, Mailgun, Mailchimp. Because these guys are more robust for sending large amounts of emails and with that you do not have the risk of your server’s IP being taxed spam.

  • So, I do the process of sending by php, ajax is only to not have to leave the page, but I send the return to Success: Function (msg) { $('#resultVerificandoEmail'). html(msg); } from jquery, which I’m analyzing how to do even in php, but I still can’t think of a logic for php and mysql for each reply send to ajax and then repeat the process until it’s over.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.