Solved

Server Push to update progress

Posted on 2009-04-10
2
386 Views
Last Modified: 2013-12-12
I'm working on a project where hitting SUBMIT starts running code that does analytics on the database. It's a long process 30 some queries, with some of them taking 5 minutes or more. I would like to display in a textArea where we are in the process ex:
  step 1 - aggregating by zip
  step 2 - calculating usage statistics
         ... etc
I'd like to do this as simply as possible. Maybe adding a row to a database table as each step begins and then have a  " Refresh Progress" button.  Kindofa rinkydink solution. I gather AJAX can do this more elegantly - But never having worked with AJAX I don't know how big a deal it would be. Can anybody provide me with some clear sample code I could adapt.
0
Comment
Question by:studioEtc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 24122406
I agree - with a long running script, some kind of AJAX reporting mechanism is a good way to go.  The "poor mans" version of the "in progress" script is the animated GIF of a clock, but it sounds like what you need is more sophisticated and easier to look at over a longer period of time.

I cannot remember where I got this, but I keep it in my teaching library.  This is signed "Rasmus" which makes me think probably "Lerdorf" of PHP fame.  It illustrates the essential moving parts of AJAX, stripped of all the fluff and shown in its most elemental way.  Have a look and see if it makes sense to you.

best regards, ~Ray
I find a lot of this AJAX stuff a bit of a hype.  Lots of people have
been using similar things long before it became "AJAX".  And it really
isn't as complicated as a lot of people make it out to be.  Here is a
simple example from one of my apps.  
 
First the Javascript:
 
function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}
 
var http = createRequestObject();
 
function sndReq(action) {
    http.open('get', 'rpc.php?action='+action);
    http.onreadystatechange = handleResponse;
    http.send(null);
}
 
function handleResponse() {
    if(http.readyState == 4){
        var response = http.responseText;
        var update = new Array();
 
        if(response.indexOf('|' != -1)) {
            update = response.split('|');
            document.getElementById(update[0]).innerHTML = update[1];
        }
    }
}
 
This creates a request object along with a send request and handle
response function.  So to actually use it, you could include this js in
your page.  Then to make one of these backend requests you would tie it
to something.  Like an onclick event or a straight href like this:
 
  <a href="javascript:sndReq('foo')">[foo]</a>
 
That means that when someone clicks on that link what actually happens
is that a backend request to rpc.php?action=foo will be sent.
 
In rpc.php you might have something like this:
 
  switch($_REQUEST['action']) {
    case 'foo':
      / do something /
      echo "foo|foo done";
      break;
    ...
  }
 
Now, look at handleResponse.  It parses the "foo|foo done" string and
splits it on the '|' and uses whatever is before the '|' as the dom
element id in your page and the part after as the new innerHTML of that
element.  That means if you have a div tag like this in your page:
 
  <div id="foo">
  </div>
 
Once you click on that link, that will dynamically be changed to:
 
  <div id="foo">
  foo done
  </div>
 
That's all there is to it.  Everything else is just building on top of
this.  Replacing my simple response "id|text" syntax with a richer XML
format and makine the request much more complicated as well.  Before you
blindly install large "AJAX" libraries, have a go at rolling your own
functionality so you know exactly how it works and you only make it as
complicated as you need.  Often you don't need much more than what I
have shown here.
 
Expanding this approach a bit to send multiple parameters in the
request, for example, would be really simple.  Something like:
 
  function sndReqArg(action,arg) {
    http.open('get', 'rpc.php?action='+action+'&arg='+arg);
    http.onreadystatechange = handleResponse;
    http.send(null);
  }
 
And your handleResponse can easily be expanded to do much more
interesting things than just replacing the contents of a div.
 
-Rasmus

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24149238
Thanks for the points - it's a great question! ~Ray
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Check if field exists SPUtility 5 47
Centos webserver sending tons of spam 27 33
Developers / Staff Setup 10 41
php non-object 7 26
This article discusses how to create an extensible mechanism for linked drop downs.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question