Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Issue with xmlhttprequest failing (very weird)

Status
Not open for further replies.

zinja

MIS
Nov 14, 2002
149
US
I cannot figure this out. I am fairly new to this whole idea of passing data from javascript to php. I have a function that works most of the time, but when I remove an alert() statement in javascript, it fails. Here is my code:

Code:
function newtrans(mydept,myprice)
{
	if (buffer) // Only store transactions with a dollar amount
	{
		getnewtransid();
		//ajaxrequest.abort();
		ajaxrequest.open("GET", storetransurl + 'id=21' + '&dept=' + escape(mydept) + '&price=' + escape(myprice), true);
  		ajaxrequest.onreadystatechange = handleHttpResponse;
  		ajaxrequest.send(null);
	}
}

function getnewtransid()
{
	ajaxrequest.open("GET", getnewtransurl + 'type=requestnew', true);
	ajaxrequest.onreadystatechange = handleHttpResponse;
	ajaxrequest.send(null);
[\code]

this next line is the issue, when I have it present, it executes without error (although I still don't get the value stored in newtransid like I need - the returned value from the xmlhttprequest).

[code]	
	alert(newtransid);
[\code]

[code]
}

function handleHttpResponse() 
{
	if (ajaxrequest.readyState == 4) 
	{
    alert(ajaxrequest.responseText);
	//alert(newtransid);
    }
}


function createXMLHttpRequest() 
{
	var ua;
     
	if(window.XMLHttpRequest) 
	{
  		try 
		{
    		ua = new XMLHttpRequest();
        } 
		catch(e) 
		{
  			ua = false;
   		}
   	} 
	else if(window.ActiveXObject) 
	{
   		try 
		{
   			ua = new ActiveXObject("Microsoft.XMLHTTP");
   		} 
		catch(e) 
		{
   			ua = false;
   		}
   	}
   	return ua;
}

[\code]

I am hoping that someone will help me to understand two things:

1.  Why removing an alert statement would cause a failure

2.  What I need to do in order to store the value that comes back from the xmlhttprequest so that I can use it in other parts of the my javascript code.


Thanks,

LJ Wilson

LJ Wilson

My personal saying - Just remember, it can always get worse, and usually will.
 
I'll help you with #1.

check for the page status too
Code:
 if (ajaxrequest.readyState == 4 && ajaxrequest.status == 200)

It works with an alert cause it give the status time to get to 200.

[monkey][snake] <.
 
I think I found the problem. I need to set the first request type to synchronous

ajaxrequest.open("GET", getnewtransurl + 'type=requestnew', true);

becomes
ajaxrequest.open("GET", getnewtransurl + 'type=requestnew', false);

so that I get my value back before proceeding any further. Then things work properly. I am thinking that the alert statement caused things to delay enough to get a value back.

Thanks for all the help - these ajax methods are cool, but just something new for me.



LJ Wilson

My personal saying - Just remember, it can always get worse, and usually will.
 
I think I found the problem. I need to set the first request type to synchronous

That is true if you call the AJAX function often enough that there could be overlap (a new call starts before an old call stops).

[monkey][snake] <.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top