0

I am trying to pass a variable to a a function that I believe calls another function (I think) but am having problems. The variable I need to use in the second function is productid but several ways thAt I have tried have not worked. either a fix in javascript or Jquery will be great!!!

This is the line that I need the variable for

var error_url = '/ProductDetails.asp?ProductCode' + productid;

this is where the variable originates from...

var productid = form.elements['ProductCode'].value;

and here is the whole js code

function addToCart2(form, button) {
var softAdd = true;
var productid = form.elements['ProductCode'].value;
 var qstr;
 var bttnName = button.name;
 button.disabled = true;
 if (form.elements['ReturnTo']) {
 form.elements['ReturnTo'].value = "";
 }
 qstr = serialize(form, bttnName + '.x', '5', bttnName + '.y', '5');
 sendAjax('POST','/ProductDetails.asp?ProductCode=' + productid + '&AjaxError=Y', qstr , retrieveProductError2 ,displayServerError,false);
 button.disabled = false;
 return false;
 }
function retrieveProductError2(result, statusCode) {
var ele = document.getElementById('listOfErrorsSpan');
var errorIndex = result.indexOf('<carterror>');
var productIndex = result.indexOf('<ProductIndex>')
if (errorIndex > -1 && productIndex == -1) {
var error_url = '/ProductDetails.asp?ProductCode' + productid;
window.location = error_url;
} 
if (errorIndex != -1) {
 //ele.innerHTML = result.slice(errorIndex + 11, result.indexOf('</carterror>'));
}
else {
 ele.innerHTML = "";
 if (productIndex == -1) {
 sendAjax('GET','/AjaxCart.asp?GetIndex=True', '', showCart, null, false);
 }
 else {
 productIndex = result.slice(productIndex + 14, result.indexOf('</ProductIndex>'));
 sendAjax('GET','/AjaxCart.asp?Index=' + productIndex, '', showCart, null, false);
 }
}

}

asked Jul 9, 2010 at 16:01

2 Answers 2

2

The easiest way is to just move your variable declaration outside of your method. So change the declaration of product id outside your addToCart2 method. So outside of that method you do this:

var product_id;

Then inside your method remove var from product_id and it will just be an assignment and not declaration.

answered Jul 9, 2010 at 16:08
Sign up to request clarification or add additional context in comments.

2 Comments

That works but since it is uses a global variable is a very bad programming style. Also, it can lead to side effects when multiple ajax calls are called simultaneously. Since ajax is asynchronous, this is very hard to prevent.
@txwikinger I agree. It was just an easy way to get him moving on.
1

Where you pass in retrieveProductError2 as your error callback for the sendAjax call, you could instead pass in:

function(result, statusCode) { retreiveProductError2(result, statusCode, productId);}

Then change the definition of your retreiveProductError2 function to accept the additional parameter.

answered Jul 9, 2010 at 16:29

4 Comments

Can you explain as I am having trouble understanding you answer?
On line 12, you call sendAjax...one of the parameters is retrieveProductError2. Replace that with the code I gave above. Then, change line 16 to read function retrieveProductError2(result, statusCode, productId) {. You'll then have access to the productId variable inside the retrieveProductError2 function.
And make sure you spell retrieve correctly - unlike myself :)
Wow that worked like a charm!!! Had to change the variable name to productid in your example not productID but it works!!! You da man!!!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.