Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jul 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript and coldfusion

    Does somebody know how to receive 2 dimensional array from JS in coldfusion?

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Wasn't your question answered here?

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    This is what Vladdy meant in the other forum.

    Let's say your 2d array looks like this:
    Code:
    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
    Code:
    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:
    Code:
    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.

  • #4
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you're using a ColdFusion version later than 4.0, (which I pray you are) you can use WDDX 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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •