I have a simulation of a loop where emails are sent out. Each email takes several seconds, so I'd like to have an indicator that shows a cumulative count as each has been sent. Some have told me that jQuery and AJAX cannot be used to do this. Has anyone had any experience in showing progress of a loop?

Again, the code below is a simulation.

Thank you...

Todd

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  <head>
    <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8" />
    <title>Learn ajax</title>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
  </head>
  <body>
  <?php
  $rvars = $_REQUEST;
  if (isset($rvars["count"])) $count = $rvars["count"];
  else $count = 0;
  if (isset($rvars["start"])) {
    for ($i = 0; $i < 100; $i++) {
      // Actually, each loop would be sending a email
      $count = $i;
      $rvars["count"] = $count;
    }
  }
  ?>
  <h1> Test AJAX counter</h1>
  <form id="countform" method="post">
    <div>
    <label for="start_button">Press to start sending emails: </br></label>
    <input type="submit" value="Start" id="start_button" name="start" />
    <input type="hidden" value=" <?php $rvars['count'] ?>
    </div>
  </form>
  <div id="count_results">Count: <?php echo $count ?></div>
  </body>
</html>

    If your loop that sends emails could store its current value of $i either in a database or in a file somewhere, you could set up a Javascript timer function to query a separate PHP script that could check that database or file to see what progress was being made.

      As sneakyimp says. jQueryUI has a progress bar widget. JavaScript's setTimeout can be used to trigger an XHR request to the server to ask "how far have you got?".

      One potential snag is that if you're using the default file-based sessions, PHP will lock the session file when the session starts and won't release it until the session is closed. While it's locked, other scripts that want to run in the same session will be blocked. The script doing the long-running process will need to lock the session as little as possible so that the progress update requests have a chance to get at the session file. (Which is why it's probably more convenient to store the progress of the job in the database: if you store it in the session data, then the long-running process will have to repeatedly (i.e., every time it does something that involves a progress update) start the session (to update the progress) and then close it (to give the progress update request a chance to look at the session file).)

        Another consideration is how to make sure that the progress being reported for a particular user corresponds to the right uploading file. Keep in mind that many users might be uploading at files and you don't want to be showing Alice the progress of Bob's upload because maybe Bob's file is 1,000 times as large as Alice's file. Or someone might be uploading multiple files at once: some big, some small.

        Databases are typically really good at this many-processes-at-once situation, so as Weedpacket suggested, it would probably be easiest to use a database.

        A quick google search yields a few helpful links:
        http://php.net/manual/en/session.upload-progress.php
        http://www.sitepoint.com/tracking-upload-progress-with-php-and-javascript/
        http://www.johnboyproductions.com/php-upload-progress-bar/

          Write a Reply...