views:

52

answers:

2

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);
    }
}

}

+2  A: 

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.

spinon
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
@txwikinger I agree. It was just an easy way to get him moving on.
spinon
+1  A: 

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.

sje397
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.
sje397
And make sure you spell retrieve correctly - unlike myself :)
sje397
LOL, ok will do
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!!!