[tech] Prototype Ajax.Request and arrays as parameters

I just ran into the weirdest problem when using prototype. Here’s a post in hopes that if someone else has the same problem they know how to fix it.

I need to pass an array as a parameter to my php script. I’m doing this using Ajax.Request. The problem is that is not passing one variable with multiple values, but the same variable multiple times with multiple values.

So its passing this:

arrayToPass : 1
arrayToPass : 2
arrayToPass : 3

Instead of the correct way:

arrayToPass : [1,2,3]

I think the problem might be the array I’m passing being possibly unconventional. I’m selecting all the checkboxes in a list that have been checked with this nifty code I found online:

$$('#NameOfTheForm input.classNameOfTheInput').each(function(box){
arrayOfValues.push(box.getValue());
}

Here’s the Ajax.Request:

new Ajax.Request(PathToYourAjaxFile, {
parameters: { arrayToPass :
arrayOfValues },
evalScripts : true,
onSuccess : function () {},
onFailure : function () {},
}

The only solution I found was to enclose the variable arrayOfValues in single quotes like so. Even though its actually passing a comma separated string, at least its passing it.

arrayToPass : ''+arrayOfValues+''

And that’s my techie complaint for the day.

Permalink | Comments | July 2nd, 2008 at 2:45 pm | 2 years ago
Tags: tech