Showing the result according to the load and not waiting for the script to finish displaying the results on the screen

3

My question is as follows: the code is working as expected the problem when I put a large email list (100) the page gets all in white "loading" and only returns me the result after the script ends to be executed, I need a solution that allows me to show the result according to the loop and passing on the screen.

I thought of using jQuery to be able to automate sending 1 by 1 and go showing the result I think that way the server will not overload.

I do not know what to do now, but I need some help from at least knowing what to search for and what function should I use to load the data dynamically.

The code is this here below:

 $email = explode("\n", $to);
 $headers .= "From: ".$nome." <".$de.">\r\n";
 $message = stripslashes($message);

 $i = 0;
 $count = 1;
 while($email[$i]) {
     $ok = "Ok!";
     if(mail($email[$i], $subject, $message, $headers))
       echo "Aguarde: $count <b>".$email[$i]."</b> <font color=red>Email Enviado </font> <br><hr>";
     else
       echo "Aguarde!: $count <b>".$email[$i]."</b> <font color=red>Email não enviado. </font><br><hr>";
     $i++;
     $count++;
 }
 $count--;
 if($ok == "ok")
   echo "";
    
asked by anonymous 12.01.2015 / 02:40

2 answers

3

If the intention is to go see each mail that was sent on the client side then a solution is jQuery / JavaScript using AJAX to control the sending speed. For example, sending a new email to each success of the previous AJAX request, or even creating a short wait time between each submission.

In this case it would be necessary to have the emails in an array for example on the client side, or to only have the total number of mails and to send ajax requests with the number that should be sent each time.

So in PHP it would be important to export a global variable with the mails or with the maximum number:

<?php echo '<script>var totalMails = '.$total_mails.';</script>'; ?>

and in a separate PHP file (which would be to handle ajax requests):

if (!isset($_POST['nr'])) die('Erro no pedido ajax');
$i = $_POST['nr'];
if(mail($email[$i], $subject, $message, $headers))
     echo "Aguarde: $count <b>".$email[$i]."</b> <font color=red>Email Enviado </font> <br><hr>";
 else
     echo "Aguarde!: $count <b>".$email[$i]."</b> <font color=red>Email não enviado. </font><br><hr>";

And the jQuery / JavaScript part would look something like:

var totalMails = 100; // isto vem do PHP
function enviarPedido(nr) {
    $.ajax({
        type: "POST",
        url: "enviador.php",
        data: {
            nr: nr
        }
    }).done(function (msg) {
        $('#div_resultado').append(msg);
        if (totalMails > 0) enviarPedido(totalMails--);
    });
}
enviarPedido(totalMails--); // assim começa em 100 e vai diminuindo até 0
    
12.01.2015 / 08:50
0

This is a very easy example of interpretation and adptation. See:

<?php
    ob_implicit_flush(true);
    set_time_limit(0);
    $results=1200;
    for($i=0;$i<1200;$i++)
    {
        ob_start();
        echo $i;
        sleep(1);
        ob_end_flush();
        ob_flush();
    }
?>
    
31.08.2016 / 23:52