Rick
Rick

Reputation: 441

JQuery function from within PHP While Loop

I have a PHP WHILE loop I'm using to send a batch of emails.

What I'd like to do is have a DIV show my user the current count of emails going out via jQuery.

So, as each pass is made in the loop, it would update a single div with the new count. I'm trying to avoid having a simple echo $count; in my code so it doesn't just scroll down the page. My count is going to be approx 3000.

Thanks! Rick

Upvotes: 0

Views: 559

Answers (5)

Rick
Rick

Reputation: 441

All the answers were great and I really appreciate it! They all got me to thinking and I've come up with a new approach to my issue. Originally, I was going to have my PHP script run the MAIL() function while the user waited. This poses problems of it's own... Like, what if the user shuts down their browser?

So, what I'm going to do is save the info to my database, and then run cron jobs every so often, looking for unsent mail. When it finds some, it will send it. With this approach, I can send important mail blasts immediately (or within a reasonable amount of time), and not-so-important mail in the wee hours of the morning as to not clog up my server. Then, after my mail has successfully (or unsuccessfully) sent, I can send an update to my user.

Thank you to everyone that responded! It was all the answers together that got my brain working...

Rick

Upvotes: 0

tereško
tereško

Reputation: 58444

You are doing it wrong.

Instead you should fork the script and let it run in the background. Said script should update a status file (preferably, once for every 10 e-mails) and store there either current progress, or message done.

On the JavaScript side of things you wither directly read the content of that file ( if you keep it somewhere inside document_root ) , or execute a simple php script which read that file and returns you the status update.

P.S. jQuery isn't really required for any of it.

Upvotes: 1

abcde123483
abcde123483

Reputation: 3905

You basically can't do what you are suggesting in the way that you are suggesting.

Instead you could look into something of this structure: have a javascript function that triggered a php file to send 10 mails and then updated saying, I have now sent out 10 more mails out of x.

You could do the triggering using jquery's post() method for calling another file with some parameters and then receiving an answer.

Upvotes: 1

Mathieu Dumoulin
Mathieu Dumoulin

Reputation: 12244

You are mixing two execution phases here. PHP executes serverside and sends back HTML. What you need to understand here is that your PHP should output some HTML + jQuery that will then use AJAX to call back another script that will do simple and fast units of work such as sending email:

  1. index.php sends back HTML+jQuery
  2. jQuery AJAX's a call to process.php
  3. process.php processes one email and sends a JSON response back
  4. AJAX receives the response, treats it and calls "process.php" again until the whole process is complete.

I can't give you real examples as it would take a lot of time to write them down, but you have a good idea on the general way to do it.

Good luck


Here are a few pointers to get you started:

http://api.jquery.com/jQuery.post/

https://www.php.net/json_encode

https://www.php.net/json_decode

With this, you should be able to get started a little faster than by searching yourself completly.

Upvotes: 2

MrJ
MrJ

Reputation: 1938

You'd have to have a PHP script sending the emails, writing a value to a database each time an email is sent (or in multiples of 10, 20, 30, etc, so you don't overkill the database)

Then you need to create another PHP which can display the value within the database

Finally, using jQuery on a (possible) 3rd page, you could do an ajax request of the 2nd page getting the output of the 2nd page, where you re-request the ajax request every X seconds

Upvotes: 1

Related Questions