Hi everyone

I'm doing a very simple click count funktion with Ajax where I put some data in a mySQL database when some links are clicked:

onClick="UpdateSQL('1', '190', 'divname', 'message');"
function UpdateSQL(KundeID, IP, sideElement, kaldMessage) {
     document.getElementById(sideElement).innerHTML = kaldMessage;
     try {
     req = new XMLHttpRequest(); /* f.eks. Firefox */
     } catch(e) {
       try {
       req = new ActiveXObject("Msxml2.XMLHTTP");  /* IE-versioner */
       } catch (e) {
         try {
         req = new ActiveXObject("Microsoft.XMLHTTP");  /* IE-versioner */
         } catch (e) {
          req = false;
         } 
       } 
     }
     req.onreadystatechange = function() {svarUpdateSQL(sideElement);};
     req.open("GET","JavaClickCount.php" + "?KundeID=" + KundeID + "&IP=" + IP,true);
     req.send(null);
  }
     
  

function svarUpdateSQL(sideElement) {
   var output = '';
   if(req.readyState == 4) {
      if(req.status == 200) {
         output = req.responseText;
         document.getElementById(sideElement).innerHTML = output;
         }
      }
  }

And the file called puts the data in the database..
This all workes fine, but if I click the link again, it doesn't work... It works in Firefox, if I click the link or button or whatever it puts data in the database multiple times...
But in IE7 and 8 it only works on the first click, and when you have to close the browser window and and go to the url again make it count one more click...

Does anyone know why it does like that... Do I have to somehow close the script/ajaxdiv in the end of the funktion or ? what's the trick.

best
Michael

Hasn't anyone seen this before ?

I've never heard of the problem you're describing.
I always do it slightly different myself, though:

function ajaxRequest(strURL, strAction, strAdd, object) {
    var xmlHttpReq = false;
    var strRequestUrl = strURL;
    var self = this;
    
    if(strRequestUrl=="") strRequestUrl = strAjaxRequestFile;
    if (window.XMLHttpRequest) { // Mozilla/Safari
        self.xmlHttpReq = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) { // IE
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    strRequestUrl += "?ajaxAction=" + strAction;
    if(strAdd!="")
        strRequestUrl += "&a=" + strAdd;
   
    self.xmlHttpReq.open('GET', strRequestUrl, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
		updatePage(self.xmlHttpReq.responseText, strAction, object);
        }
    }
    try {
		self.xmlHttpReq.send(null);
	 } catch(err) { }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.