1

I have the following simplified while loop code:

while($row = mysql_fetch_array($result)) {

    mail($to, $subject, $newMessage, $headers);

}

Is it possible to display a text while the loop goes on, such as "Sending email 1 from 100", then "Sending email 2 from 100", etc...? When the while loop finishes, clear the text and show for example, "All mail was sent!".

I don't know how to explain it better, what I can easily do is print that text every time we enter the loop, but then the page ends up with 100 texts printed.

I'd like something like a floating div that updates while in the loop, then dissapears.

Hope I have made myself more or less clear!

Thankss!

5
  • 2
    I fear, that not completely do-able at the server side, as the PHP is completely parsed and then given as response to the browser request. Let's see what others have to tell. Commented Aug 25, 2012 at 12:38
  • This would be done with Ajax. Commented Aug 25, 2012 at 12:39
  • 1
    No , It can not be done using php alone. Commented Aug 25, 2012 at 12:39
  • 1
    Sending a hundred emails right from the request seems like a bad idea to begin with. That's a job for a background queue/worker script. Commented Aug 25, 2012 at 12:41
  • Yes, I know it should be done in the background. With AJAX could be possible? I need a solution, it doesn't matter its PHP or not, could @darma or fluty help with the code? Thx! Commented Aug 25, 2012 at 12:45

1 Answer 1

0

You'll have to fill in but so you get the basics, example with jQuery ajax :

Javascript :

var count = 0;
function sendNextEmail(){
    $.ajax({
        url : 'send-email.php?count=' + count,
        response : function(response){
            //TOTO : update UI
            count++;
            sendNextEmail();
        }   
    });
}

PHP send-email.php :

$count = intval($_GET['count']);
//TODO : PHP code to send count-th email

Edit : you'll need to deal with potential security issues (input parameters, direct calls to the PHP script...), as well as the fact that the whole thing may easily be interrupted by the user.

Sign up to request clarification or add additional context in comments.

2 Comments

I'll definitely give it a try! Thank you!! Its a script I'll be running from an admin environment where only I will be logged in, no worries! Thanks again!
I can't seem to get it working... Could you please help me a bit further with a complete example? Thanks again!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.