function update(value1)
{
doAjax("behind_scan.php" , "id="+value1);


}


function doAjax(url , str )
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
url=url+"?"+str;
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function stateChanged()
{
if (xmlhttp.readyState==4)
{
//document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
//alert(xmlhttp.responseText)
document.forms.myform.scanner.value = "";
document.forms.myform.scanner.focus();

}
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}

This pages calls a function on a separate php page to run 2 mySQL queries, the value must be put in the text 3 times in quick succession before the database is altered.

If you think this code is not the problem could you please say it. Ive tried all the other functions and they seem to be fine, AJAX is my weakness but I need it for the main functionality of my website. I NEED THIS TO WORK!!!!!!! :) :)

Dani AI

Generated

The behavior described by (needing to send the value multiple times) usually points to two simple causes: a shared global XHR object being overwritten when requests fire in rapid succession, and GET-request caching from the browser. As asked, the event that calls update also matters — calling the function on the wrong event (for example keydown) can send the previous input value instead of the current one.

Root cause summary:

  • A single global xmlhttp instance gets replaced by each new call, so earlier requests’ callbacks see the wrong object or never run correctly.
  • Browsers may cache identical GET requests; the server never sees some of the requests.
  • If the caller uses keydown (or another premature event), the sent value may not be the intended one.

Concrete fixes to apply:

  • Create a fresh XMLHttpRequest per call (local variable), and use a closure for onreadystatechange so the handler always references the correct request object.
  • Add a cache-buster (timestamp or unique token) to GET URLs, or switch to POST for state-changing operations.
  • Check xhr.readyState === 4 and xhr.status === 200 before touching the page.
  • Ensure the event that invokes the call supplies the correct value (use keyup, input, or pass the element value explicitly).

Example pattern (replace names to match the page):

function sendRequest(url, params, callback) {
  var xhr = new XMLHttpRequest();
  var sep = url.indexOf('?') === -1 ? '?' : '&';
  url = url + sep + params + '&_=' + (new Date()).getTime();
  xhr.open('GET', url, true);
  xhr.onreadystatechange = function () {
    if (xhr.readyState === 4) {
      if (xhr.status === 200) callback(xhr.responseText);
      xhr.onreadystatechange = null;
    }
  };
  xhr.send(null);
}

Troubleshooting checklist:

  • Use the browser DevTools Network tab to confirm how many requests are sent and what responses/status codes arrive.
  • Have the PHP script return a simple success token and proper HTTP status so failures are visible.
  • Prefer POST for database updates and encode parameters via encodeURIComponent.

Recommended Answers

All 2 Replies

the value does not get passed all of the time.... what could cause this ???
:( :( :(

How are you calling function update?

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.