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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AJAX code??? won't work in IE more than once with the same data

Status
Not open for further replies.

jjanes

Programmer
Jul 29, 2011
3
US
Hi I have a peculiar issue with IE and AJAX. I have a certain script that works fine the data is processed the for the first time but if it is run with the same data it fails to operate. This failure only occurs in IE.

It is part of the checkout process on our website, The issue occurs in the checkout area where you enter the zip code in IE you will see the issue if you enter the data from the example below. You will have to select an item and add it to the cart in order to get to the area that has the issue.

Here is a typical situation where I see a failure again only in IE.
you go to the checkout process and enter a shipping option and a zipcode say 91401; it processes normally. Processing normally means calling a php script that sends a query to a mysql database to get a valid value for calculating a sales tax. In this case becauseit is a California zip code and we are in California it comes back with a value and the tax field in the form is updated.
If the zip code is not in California it returns with 0 and that is updated in the tax field. such would be the case if 55056 was entered for the zipcode and again it works just fine, the first time.

If, however, I enter 91401 it comes back and the field is updated. I then change it to 55056 the field changes to zero, again correct, I then change it to 91402 it still process correctly.

Now when I do this same process again in a different session I start with 91401, it works, I change it to 55056, it still works , I change it back to 91401 and nothing happensi change it again to say 91402 and it works this time if I change it back to 55056; nothing, 91401; nothing, 91402; nothing, but again if I enter 91403 it works and a value is returned.

Again this is only in IE

Here is my code for my AJAX calls and the PHP script that processes the database call


javascript
Code:
function getHTTPObject(){
	if (window.ActiveXObject)
		return new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest)
		return new XMLHttpRequest();
		else {
			alert("Your browser does not support AJAX.");
			return null;
		}
}


function doWork5(subtotal){
	error=0;
	if( ( (document.cartform.shipping[0].checked)  || (document.cartform.shipping[1].checked) || (document.cartform.shipping[2].checked) ) ==0) {
		alert("You need to select a shipping option");
		error=1;
		}

//	data =parseFloat(document.getElementById('shippingval').value.substr(1));
	zipdata =document.getElementById('zip').value;

	if (zipdata == '#####') {
		alert("You need to enter a Zip Code");
		error=1;
	}
	
	if(error==1) {
		return;
	}

//	value=data + parseFloat(subtotal);

	value= parseFloat(subtotal);
	httpObject5 = getHTTPObject();


	if (httpObject5 != null) {
		httpObject5.open("GET", "istaxable.php?taxabletotal="+value+"&inputText="+zipdata, true);
		httpObject5.send(null);
		httpObject5.onreadystatechange = setOutput5;
	}
}

function setOutput5(){
	if(httpObject5.readyState == 4){
		document.cartform.taxval.value="$" + parseFloat(httpObject5.responseText);
		grandtotal = parseFloat(httpObject5.responseText) + parseFloat(document.cartform.ordersubtotal.value.substr(1)) + parseFloat(document.cartform.shippingval.value.substr(1));
		document.cartform.ordertotal.value="$" + grandtotal.toFixed(2);
	}
}

php script istaxable.php
Code:
$subtotal=$_GET['taxabletotal'];
$zip=$_GET['inputText'];
$query1="SELECT distinct STATE_A from us2 WHERE STATE_Z='".$zip."'";
$result = mysql_query($query1)
	or die(mysql_error());
$row = mysql_fetch_array($result, MYSQL_BOTH);
if($row['STATE_A']=="CA") {
	$taxval=($subtotal*.0725);
	printf("%01.2f", $taxval);
}
else {
	if(mysql_num_rows($result)==0) {
		echo "";
	}
	else {
		echo"0.00";
	}
}

Subsequent calls to other functions with the same data after this call is processed are processed correctly.
No error is ever indicated that I can see.
In my testing I have added alert statements to verify the data is being sent in correctly and it is.

It appears to just decide not to process the php script some of the time

I guess I say thanks to IE for another buggy thing

Any help or assistance will be greatly appreciated

thanks
Jeff
 
Hi

I have no Explorer so I can not check it, but it sounds like a caching issue.

But you could find out easily if my supposition is correct. After checking the [tt]readyState[/tt] property also check the [tt]status[/tt] property. It will contain the HTTP response code, which for successful requests should be '200', meaning "Ok". According to my theory, in Explorer, when it fails, this will be '304' meaning "Not Modified" and the [tt]responseText[/tt] property will be empty string.
Code:
[b]function[/b] [COLOR=darkgoldenrod]setOutput5[/color][teal]()[/teal][teal]{[/teal]
    [b]if[/b][teal]([/teal]httpObject5[teal].[/teal]readyState [teal]==[/teal] [purple]4[/purple][teal])[/teal][teal]{[/teal]
        [highlight][b]if[/b] [teal]([/teal]httpObject5[teal].[/teal]status [teal]!=[/teal] [green][i]'200'[/i][/green][teal])[/teal] [teal]{[/teal][/highlight]
          [highlight][COLOR=darkgoldenrod]alert[/color][teal]([/teal][green][i]'This is bad.[/i][/green][lime][i]\n[/i][/lime][green][i]HTTP Response Status Code : '[/i][/green] [teal]+[/teal] httpObject5[teal].[/teal]status[teal])[/teal][/highlight]
          [highlight][b]return[/b][/highlight]
        [highlight][teal]}[/teal][/highlight]
        document[teal].[/teal]cartform[teal].[/teal]taxval[teal].[/teal]value[teal]=[/teal][green][i]"$"[/i][/green] [teal]+[/teal] [COLOR=darkgoldenrod]parseFloat[/color][teal]([/teal]httpObject5[teal].[/teal]responseText[teal]);[/teal]
        grandtotal [teal]=[/teal] [COLOR=darkgoldenrod]parseFloat[/color][teal]([/teal]httpObject5[teal].[/teal]responseText[teal])[/teal] [teal]+[/teal] [COLOR=darkgoldenrod]parseFloat[/color][teal]([/teal]document[teal].[/teal]cartform[teal].[/teal]ordersubtotal[teal].[/teal]value[teal].[/teal][COLOR=darkgoldenrod]substr[/color][teal]([/teal][purple]1[/purple][teal]))[/teal] [teal]+[/teal] [COLOR=darkgoldenrod]parseFloat[/color][teal]([/teal]document[teal].[/teal]cartform[teal].[/teal]shippingval[teal].[/teal]value[teal].[/teal][COLOR=darkgoldenrod]substr[/color][teal]([/teal][purple]1[/purple][teal]));[/teal]
        document[teal].[/teal]cartform[teal].[/teal]ordertotal[teal].[/teal]value[teal]=[/teal][green][i]"$"[/i][/green] [teal]+[/teal] grandtotal[teal].[/teal][COLOR=darkgoldenrod]toFixed[/color][teal]([/teal][purple]2[/purple][teal]);[/teal]
    [teal]}[/teal]
[teal]}[/teal]
If the above addition confirms the caching issue, we already have threads here dealing with solutions, for example thread1600-1439349 .


Feherke.
 
feherke, thnaks for that response so I did two things I added your code snippet to my code and it was NOT the issue. So I add the alerts prior to the readyState check just to see the values and it appears that the call to setOuput5 is not even being made. I am adding the code that actually make the original call.

Code:
        <table border=0>
        	<tr>
	    		<td><font color='#ff0000'>&#8226;</font>Please Enter Your Zip Code&nbsp;&nbsp;<input name='zip' id='zip' type='text' value='#####' maxlength=5 onChange='setotherzips()'></td>
    			<td><font size=+2 color='#ff0000'>&#8226;</font><input name='chktax' id="chktax" type='button' value='Determine Tax'style='width:110px;' onclick='doWork5("<?PHP echo $total; ?>");' /></td>
        	</tr>
        </table>

This should be called when ever the Determine tax button is clicked and prior tests indicated it was but when the variable has been previously entered if doWork5 is called the AJAX request is never reached but again prior testing indicated it was.

In this last test if I entered a new zip code I saw all the alerts with the different status' but when I re-entered a zip code I never saw any alerts at all. Just nothing and again it was only in IE
 
Hi

Ah, so the browser not even asks the server about status change, just uses the cached data. So I would say, just add those codes mentioned in thread1600-1439349 and should be fine. The simplest of those is this :
Code:
    [b]if[/b] [teal]([/teal]httpObject5 [teal]!=[/teal] [b]null[/b][teal])[/teal] [teal]{[/teal]
        httpObject5[teal].[/teal][COLOR=darkgoldenrod]open[/color][teal]([/teal][green][i]"GET"[/i][/green][teal],[/teal] [green][i]"istaxable.php?taxabletotal="[/i][/green][teal]+[/teal]value[teal]+[/teal][green][i]"&inputText="[/i][/green][teal]+[/teal]zipdata[teal]+[/teal][green][i]'&'[/i][/green][highlight][teal]+[/teal][b]new[/b] [COLOR=darkgoldenrod]Date[/color][teal]()[/teal][/highlight][teal],[/teal] [b]true[/b][teal]);[/teal]
        httpObject5[teal].[/teal]onreadystatechange [teal]=[/teal] setOutput5[teal];[/teal] [gray]// better reverse...[/gray]
        httpObject5[teal].[/teal][COLOR=darkgoldenrod]send[/color][teal]([/teal][b]null[/b][teal]);[/teal] [gray]// ... these two lines, like this[/gray]
    [teal]}[/teal]


Feherke.
 
Thank you very much it works great. I still find it amazing as how different the main browsers operate it is so frustrating. Caching was not one of the things I was expecting

Thanks again

Jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top