1

I am making a script that sends newsletters out. The process for sending mail is that the users clicks "send" and this dispatches an AJAX Request to sendMail.php. SendMail.php simply takes the message and sends it to all the users in a database.

When the user clicks send a dialog also pops up, and what I want to achieve is the server sends back its current progress to the client in real time, so that I can update a progress bar and tell the client exactly when all the mail has been sent.

Im not exactly sure what I should be looking for or how to go about doing this. I am using jQuery if this is any help

Apologies for any vagueness, this is new to me so I am not sure what sort of stuff you need to know, just ask if you need me to elaborate :)

Cheers :)

EDIT: There appears to be some confusion, I am not looking to make a simple AJAX request, I can do this fine. What I am struggling with is sending data back to the client incrementally from the server.

3 Answers 3

1

My way of going at it is assigning a UUID to the request. The server should update the progress on the session using the UUID as key. Then the Javascript can query the server repeatedly for the progress using the ajax jquery interface.

So that overall the script will look something like this (pseudo code)

<script>

function pollServer(uuid)
{
   $.ajax({
         url:'/pollProgress',
         // the response is the progress
         success:function(result){ 
                if (progress == 100) { ... /* finish */ } 
                else
                {
                     ... // update the progress somewhere.. for example $("#progress").text(result)
                     setTimeout(function(){pollServer(uuid)},100);
                }
        }
}
// first request to server will be the "send" request
$.ajax({
   url:'/sendMail',
   //  the server should send the UUID as the result
   success:function(result){ pollServer(result)}
})
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

Check out jQuery's $.post() functionality here: http://api.jquery.com/jQuery.post/. This allows you to send a request to a server side script and receive a real time result.

Comments

0

Here is an overview: http://ajaxpatterns.org/Progress_Indicator

Here is some detailed AJAX code: http://www.redips.net/javascript/ajax-progress-bar/

Here is a good way to display the progress bar: http://docs.jquery.com/UI/Progressbar

Comments

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.