Welcome
Members
Calendar
Mailing List
Links
Perl 101
Perl 201
Snippets
Projects
Books
Describe 'AjaxExample' here.

  #!/usr/bin/perl

  use strict;
  use CGI;


  my $cgi = new CGI;

  my $num = $cgi->param('num');

  print "Content-type: text/html\n\n";

  if($num) {

    print ++$num;

  } else {

    print q|
    <html>
      <head>
        <script type="text/javascript">

          function new_request() {
            if (window.XMLHttpRequest) {
              req = new XMLHttpRequest();
            } else if (window.ActiveXObject) {
              req = new ActiveXObject("Microsoft.XMLHTTP");
            }
            return req;
          }   

          function do_request(url, callback) {
            req = new_request();
            if(req != undefined) {
              req.onreadystatechange = function() {
                if (req.readyState == 4) { // only if req is "loaded"
                  if (req.status == 200) { // only if "OK"
                    callback(req.responseText);
                  } else {
                    alert("AJAX Error:\r\n" + req.statusText);
                  }
                }
              }
              req.open("GET", url, true);
              req.send("");
            }
          }

          function go() {
            alert("GO!");
            do_request('http://thelackthereof.org/dev/pm-2006.01.25/ajax.pl?num=0', shownum);
          }

          function shownum(num) {
            alert("Got callback, now displaying number...");
            counter = document.getElementById('counter');
            counter.innerHTML = num;
            //do_request('ajax.pl?num=' + num, shownum);
            alert("Re-initializing HTTP request");
            do_request('http:thelackthereof.org/dev/pm-2006.01.25/ajax.pl?num='+num, shownum);
          }

        </script>
      </head>
      <body>
        <h1>AJAX DEMO
        <div id="counter">0
        <a href="#" onclick="go()">GO!
      </body>
    </html>


    |;
  }




· Home Page · Edit This Page · Reverse Links · Expand Links · Recent Changes