...

View Full Version : Javascript and coldfusion



lolena1888
07-12-2004, 08:12 PM
Does somebody know how to receive 2 dimensional array from JS in coldfusion?
:(

Willy Duitt
07-12-2004, 08:25 PM
Wasn't your question answered here (http://www.webdeveloper.com/forum/showthread.php?threadid=39357)?

glenngv
07-13-2004, 07:49 AM
This is what Vladdy meant in the other forum.

Let's say your 2d array looks like this:


var arr = [
[1,2,3],
[4,5,6],
[7,8,9]
];

Then this function will send the value to the hidden field. The form must be submitted to a cold-fusion page to process the data


function submitForm(){
document.theForm.theField.value = arr.join('|'); //results to 1,2,3|4,5,6|7,8,9
document.theForm.submit();
}

If your array is not numbers but strings and can have commas in it, you have to do it the hard way:


Array.prototype.toString=function(delim, delim2){ //delim=delimeter for outer array; delim2=delimiter for inner array (both default to comma if not specified)
var strArray = '';
var strInnerArray;
if (delim==null || typeof delim == "undefined") delim=",";
if (delim2==null || typeof delim2 == "undefined") delim2=",";
for (var i=0;i<this.length;i++){
if (this[ i ] instanceof Array){ //2d array
strInnerArray = this[ i ].toString(delim2);
strArray += (i>0)?delim+strInnerArray:strInnerArray;
}
else {
return this.join(delim); //single dimension array
}
}
return strArray;
}

//sample usage:
var arr = [
["blah,blah","bleh-bleh"],
["foo'foo"],
["hello","hello;world"]
];

function submitForm(){
document.theForm.theField.value = arr.toString('|','~'); //results to blah,blah~bleh-bleh|foo'foo|hello~hello;world
document.theForm.submit();
}

This is made possible by overriding the Array.toString method without sacrificing its original usage.

Choopernickel
07-13-2004, 02:36 PM
If you're using a ColdFusion version later than 4.0, (which I pray you are) you can use WDDX (http://www.google.com/search?q=wddx&ie=utf-8&oe=utf-8) to communicate data between the two languages.

Allaire, before they were bought by Macromedia, invented WDDX specifically for data exchange between ColdFusion and Javascript, then released all manner of proprietary rights to it by publishing it as an open source standard. It's an interesting idea, and can work quite well.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum