refresh a div in ajax

Hello all,

I am working on a newsletter managing webapp. And when I am launching the process it can take a while to finish (to send emails). So I'am currenlty looking to refresh a css div in my view to show the status but I'am stuck.

I read articles on forum regarding this issue but don't find something clear.

Any advice is welcome, thank you! :)

You'll need to use ajax.  Do you know javascript?

Yes :)

I know how to do it with "pure" javascript.

I’am just looking how to use yii to handle it :)

& if you want it to show a percent loading bar, or percent complete, etc, you will need some very fancy code.  Can't help you much there.  If you want it to simply say "loading" or something, just use JS to set a invisible div (that says "loading") to be visible.  ajax is not necessarily needed actually. &you are not giving much details

Personally I like writing my own JS versus using a frameworks features to write it for me.  why?

  • more customizable

  • more expendable

unless I am prototyping, in which case I may use framework functions

Ok thanks for your feedback :)

How can I call javascript functions inside the controller ?

Not sure what you mean.  PHP obviously can't talk directly with JS.  You probably need a hidden div in your view that says "loading".  You probably also have a link or button to send the emails.  Have jquery set the div as visible when the button/link is pressed, and send an ajax request to the email page.  Give it a large timeout.  When the request is successful, hide the div again and unhide a div that says "Successful" or something.

Just an idea.  I admit I am pretty new to jquery.

OK thank you.

But In case I want to show a progress bar. Only the controller know how many mail are sended at instant T so I will be obliged to use ajax isn't it?

You could create a polling mechanism that frequently sends AJAX requests to receive the current progress information. And the mailer would have to write somewhere, how many mails where already sent. You could use APC for this. Just some ideas…

Thank you Mike. Can you explain a bit more about you previous answer?

Theres a nifty solution with an upload progress bar that uses APC by Rasmus Lerdorf. Take a look here:

It basically uses the same concept: On the clientside you have to write code, that frequently sends a request to ask for the current progress. The response might contain a JSON object with information, how many mails where already sent, how many failed, etc.

To get this response, the mail sender process first needs to save this information somewhere. You could use APC to store this value with a specific key to the cache after each email was sent.

And in the controller that replies to your AJAX requests, you simply look in APC, what value is there and return the JSON object.

Main problem will be, to create a mailer process that forks from a PHP request and runs in background, after the mail sending has been initiated. Try to google for "PHP fork process" or something. There are solutions for it. But might get a little tricky.

thanks a lot, I will study that  ;)