Jquery ajax: start getting data on click

Hi Experts,

I have this basically code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {

$( "#delete" ).click(function() { 
  url: 'delete.php',
  success: function(data) {


<button id="delete">Delete Posts</button><br />
<span id="loader" style="display:none;">LOADING</span><br />
<span id="count"></span> <span>Posts Deleted</span>


Open in new window

It works fine but only gets the data on success. My php is running a foreach function and echoing the count as it goes. I would like to show this in the #count span, not just on success.

Any ideas?

Many thanks in advance
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
You can't

The php script has to finish first.

The server side and client side are dissociated processes. The client side makes an AJAX call to the server - the server completes the request and sends back the results - the AJAX then takes over again.

If you want to do this you would need to look at something like Comet or Node.js
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
I have not tried this, but thinking out loud, could you use some type of progress bar and have your php update the progress bar where reaching 100% triggers what would be your success function?  

If seeing the count is more important than efficiency you could get rid of the foreach and move control to a client side loop that fires a separate AJAX request for each iteration.  On the face of it that would be pretty stupid, unless you really want to display that count.

The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Julian HansenCommented:
could you use some type of progress bar and have your php update the progress bar where reaching 100% triggers what would be your success function?

Open in new window

@padas - php code cannot interact with the client side browser code.
Julian HansenCommented:
This all depens on how time intensive the loop is.

For processes that take a long time the usual practice is to have the loop processed by a background process. A script kicks it off but asynch and returns immediately.

Your client side script then regularly polls the server to get an update on the count until it is done.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
maccaj51Author Commented:
Thanks for all your help
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.