Ajex calls
==========

// global variables to keep track of the request
// and the function to call when done
var ajaxreq=false, ajaxCallback;
// ajaxRequest: Sets up a request
function ajaxRequest(filename) {
   try {
    // Firefox / IE7 / Others
    ajaxreq= new XMLHttpRequest();
   } catch (error) {
    try {
      // IE 5 / IE 6
      ajaxreq = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (error) {
      return false;
    }
   }
   ajaxreq.open("GET",filename);
   ajaxreq.onreadystatechange = ajaxResponse;
   ajaxreq.send(null);
}
// ajaxResponse: Waits for response and calls a function
function ajaxResponse() {
   if (ajaxreq.readyState !=4) return;
   if (ajaxreq.status==200) {
      // if the request succeeded...
      if (ajaxCallback) ajaxCallback();
   } else alert("Request failed: " + ajaxreq.statusText);
   return true;
}


// Calling JavaScript function
=====================

function storeInfoDSA()
{
 
   // user input on form statement   
   
   document.form1.Rfactor.value  = document.form1.mouseRandomFactor.value;
   
    // other stored information in Java Script variables
   document.form1.Speed.value    = Speedmouse;
   document.form1.Pos1.value     = StorePos1;
   document.form1.Pos2.value     = StorePos2;
   document.form1.Pos3.value     = StorePos3;

  // Ajax call back method
   
   ajaxCallback = nextTask;

  // set up url data protion of url
   var parms = "?Pos1=" + StorePos1 + "&Pos2=" + StorePos2 + "&Pos3=" +                 StorePos3 + "&Speed=" + document.form1.Speed.value +                 "&Rfactor=" + document.form1.Rfactor.value + "&User=" +                 document.form1.USERID.value  + "&Description=" +                 document.form1.laps.value;
 
   // call perl script (storeInfo.pl) located in cgi-gin
   ajaxRequest("../cgi-bin/storeInfo.pl" + parms);
 
}


// calling Perl script
==============

#!/usr/bin/perl
# connect.pl

#use strict;
use CGI ':standard';
use DBI;


my $pos1 = param('Pos1');
my $pos2 = param('Pos2');
my $pos3 = param('Pos3');
my $r_factor = param('Rfactor');
my $speed = param('Speed');
my $user  = param('User');
my $desc  = param('Description');

// information within [ ]  have to be replaced with actual values

my $dbh = DBI->connect("DBI:mysql:[database name]:[port no]", "[userId]", "[password]");
                   die "connect failed: " . DBI->errstr() unless $dbh;

// get last id from primary key database column
my $sth4 = $dbh->prepare("SELECT max(id) from results")
             or die "prepare failed: " . $dbh->errstr();

$sth4->execute() or die "execute failed: " . $sth4->errstr();

my $id=0;
my $row=0;

my @result = $sth4->fetchrow_array();

foreach $row (@result)
{
   
    $id = $row;
   
}

$sth4->finish();

$id = $id + 1;             // increase primary key value

// insert actual information into the MySql database

my $insert = $dbh->prepare("INSERT into results (id, pos1, pos2, pos3, r_factor, speed, user, description)
                        values($id, $pos1, $pos2, $pos3, $r_factor, $speed, '$user', '$desc')")
              or die "prepare failed2: " . $dbh->errstr();

$insert->execute() or die "execute failed: " . $insert->errstr();

my $result = $insert;

$insert->finish();

$dbh->disconnect();

// return result of database update

print "Content-Type: text/xml\n";
print "\n";
print "<?xml version=\"1.0\" ?>\n";
print "<results>\n";
print "<a>$result</a>\n";
print "</results>\n";


exit 0;

// Java Script call back function
=====================

function nextTask()
{
        
      var ct = 0;
      dbResults = ajaxreq.responseXML.getElementsByTagName("results");
      var sizedbResults = dbResults.length;
      results = new Array(sizedbResults);      

     // we don't need loop because we only have one row
     // however this sample shows how to retrieve multiple rows
      
      for (var i=0; i < sizedbResults; i++)
      {
          var dbFB = dbResults[i].getElementsByTagName("result")[0].firstChild.nodeValue;
          results[ct] = dbFB;
          ct = ct + 1;
          
          
      }
      
      var checkResult = results[0];

      if (checkResult != '1') window.alert("Update failed. Database error");  
      
      
}