Cheeki 0 Newbie Poster

Hey guys, im having the weirdest problem with my ajax!! below is the function that is called when ever a if statement evaluates to true. Anyway the problem im having is that this if statement

if(xmlHttp.readyState == 4)

with in the ajax functions runs when readyState is equal to 1 and when equal to 4!! so obviously its printing out twice when its only meant to print out once!!

function getFlowButtons(popUp, flow_button_id)
{
	var xmlHttp = getXMLHttp();
		
	xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState == 4)
		{
			HandleResponseNewButtonContainer(xmlHttp.responseText);
		}
	}
	
	xmlHttp.open("GET", '../ajax/ajax.php?action=setFlowButtons&value='+flow_button_id+'&value2='+numberofcontainers, true);
	xmlHttp.send(null);
}

CAN ANYONE HELP ME PLEASE!!!

Dani AI

Generated

A few quick, practical checks and fixes that usually solve this kind of duplicate-callback behavior.

First, use the browser Network panel to confirm whether one or two HTTP requests are actually sent when the event fires. If you see two requests, the client is calling your function twice (duplicate event binding, double-clicks, or code path calling getFlowButtons more than once). If you see one request but the handler runs twice, the XHR object is likely being reused or the response handler is being triggered indirectly (for example, the response handler calls code that triggers another request or re-enters the same handler).

A safer pattern is to attach a single final-completion handler instead of relying only on state changes. For example:

var xhr = new XMLHttpRequest();
xhr.onload = function() {
  if (xhr.status === 200) {
    HandleResponseNewButtonContainer(xhr.responseText);
  } else {
    console.error('XHR failed', xhr.status);
  }
};
xhr.onerror = function() { console.error('Network error'); };
xhr.open('GET', url, true);
xhr.send();

Other troubleshooting tips:

  • Add a quick trace at the top of getFlowButtons (console.count or console.trace) to confirm how many times it is invoked.
  • Ensure getXMLHttp returns a fresh XHR each call; do not reuse a single global XHR unless you explicitly abort the previous one.
  • Check HandleResponseNewButtonContainer for any code that might re-trigger the same request or insert duplicate DOM nodes; add an id/flag guard if needed.
  • See the XMLHttpRequest docs on events and ready states for details: XMLHttpRequest - MDN.
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.