• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 433
  • Last Modified:

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
1 Solution
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.

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

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.
maccaj51Author Commented:
Thanks for all your help
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now