Hi there,

I believe I am having trouble with the response that is received by the Ajax request.

I have posted something similar to this but the response returned was XML not plain text (or html). I am guessing this should be easier but I can't figure out or find a suitable method that works. (or I am just doing it wrong).

Everything works fine in Chrome and Firefox. IE7 seems to mess up. I've got as far as believing that I need to include some sort of Active X Object code for IE, but cant find how to integrate it with what I have.

Here's the code I have that works in the other browsers:

function addToBasket(itemId,itemPrice,multiplier,type)
		{
			a = itemId;
			b = itemPrice;
			c = multiplier;
			d = type;
			
			var request = new Ajax.Request
				('basket.php',
					{ method: 'get',
					  parameters: 'itemid=' + a  + '&itemprice=' + b + '&multiplier=' + c + '&producttype=' + d,
					  onComplete: responseReceived }
				);
		}
		
		
		
		// The callback function
		function responseReceived(xmlHTTP)
		{
			
		
			if(xmlHTTP.responseText=="basket error")
			{
				document.getElementById('basketerror').innerHTML = "That item is already in your basket.";
			}
			else
			{
				document.getElementById('basketquantity').innerHTML = xmlHTTP.responseText;
				document.getElementById('basketerror').innerHTML = "";
			}
		}

Any help would be greatly appreciated.

Cheers

Danny

Recommended Answers

All 11 Replies

Do you have url to your page? Most likely the issue is in basket.php

Do you have url to your page? Most likely the issue is in basket.php

http://madeby.mightydrive.co.uk/mightydrive-fonts.php

If you click one of those items it will take you to the page where you can add things to the basket. Using IE7 you'l soon notice the basket doesn't act the way it should, or how it does in chrome and FF.

Here is basket.php incase that helps.

<?php

	$itemId = $_GET['itemid'];
	$itemPrice = $_GET['itemprice'];
	$multiplier = $_GET['multiplier'];
	$type = $_GET['producttype'];
		
	include("wdc.php");	
		
	if (isset($_COOKIE["basket_session"]))
	{
		$checkQuery = "SELECT * FROM `basket_session` WHERE `basket_session_id`='".$_COOKIE["basket_session"]."' AND `basket_product_id` ='".$itemId."' AND `basket_product_type`='".$type."'";
		$checkResult = mysql_query($checkQuery);
		$checkNumRows = mysql_num_rows($checkResult);
		if($checkNumRows>=1)
		{
			echo "basket error";
			exit();
		}
		else
		{
			$query = "INSERT INTO `basket_session`(`basket_session_id`, `basket_product_id`, `basket_product_type`, `basket_product_quantity`, `basket_product_price`)
											VALUES('".$_COOKIE['basket_session']."','".$itemId."','".$type."','".$multiplier."','".$itemPrice."')";
			$basketQuery = "SELECT * FROM `basket_session` WHERE `basket_session_id` ='".$_COOKIE['basket_session']."'";
		}
	}
	else
	{
		
		$id = uniqid(rand(100000,999999),true); 
		
		$expire=time()+60*5;
		$new_cookie = setcookie("basket_session", $id, $expire);
		
		$query = "INSERT INTO `basket_session`(`basket_session_id`, `basket_product_id`, `basket_product_type`, `basket_product_quantity`, `basket_product_price`)
										VALUES('".$id."','".$itemId."','".$type."','".$multiplier."','".$itemPrice."')";
		
		$basketQuery = "SELECT * FROM `basket_session` WHERE `basket_session_id` ='".$id."'";
		
	}

	$result = mysql_query($query);
	
	$basketResult = mysql_query($basketQuery);
	$basketRows = mysql_num_rows($basketResult);
	echo $basketRows." Items";	
	
?>

Thanks for your help!!

Danny

if you are referring to the alignment, try:

document.getElementById('basketerror').innerHTML = "<div>That item is already in your basket.</div>";

Well that has fixed the alignment problem, thank you.

The bigger issue I am concerned about is when adding items to the basket, they do not add correctly, as you can see in the top right hand corner.

I still believe this to be a matter of not including the correct code for an IE ajax request/response (Active X Object stuff). I just don't know how to integrate it into my code.

Please tell me that I have it wrong if that is the case.

Any further information to help solve this would be great.

Thanks for the help so far!

Danny

instead of: var request = new Ajax.Request('basket.php', ... try: var request = new Ajax.Request('basket.php?cb='+ (new Date()).valueOf(), ...

Changed to:

function addToBasket(itemId,itemPrice,multiplier,type)
		{
			a = itemId;
			b = itemPrice;
			c = multiplier;
			d = type;
			
			var request = new Ajax.Request
				('basket.php?itemid=' + a  + '&itemprice=' + b + '&multiplier=' + c + '&producttype=' + d,
					{ method: 'get',
					  onComplete: responseReceived }
				);
		}

Hope this was what you meant.

It still works in chrome, but not in IE. The basket seems to randomly put a number of items in the basket. And more often than not there's not actually anything in there when you visit the basket page.

Current version is uploaded.

Any other idea's? Open for any and all suggestions! lol.

Danny

Hope this was what you meant.

No, that wasn't what I meant. You just needed to change line 9 of your original post to what I gave you. Everything else would be the same.

Silly me. Sorted it! Thanks very much!

Out of curiosity. And as it may be useful to me next time. What did that extra bit of code actually do to make it work?

Thanks so much for this!

Cheers

Danny

It forces IE to make the ajax request EVERY TIME, since it adds the current timestamp, which is different everytime. From the browser's view, the url is different everytime, it does NOT use the cached page. That was your problem - it was reusing the cached page.

Well I never would have figured that out :P

Thanks again!

Ya learn something new every day.

Cheers

Danny

Glad to help.

Regards,
Hielo

PS: Don't forget to mark the thread as solved.

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.