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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New Coder
    Join Date
    Dec 2003
    Location
    Bahamas
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JavaScript/Cold Fusion SQL Data xfer

    I have a Java script which populates an array . What I want to do is when I submit the page the array be available to cold fusion so that I can update my SQL 2K database via an existing cold fusion datasource. Or is there a cleaner way to get data from JS to SQL? HeLLLppp!!!!

    P.S. Does anyone know how to make Dreamweaver MX create a new window when testing scripts rather than overwriting the last used window?

  • #2
    Regular Coder
    Join Date
    Aug 2002
    Location
    São Paulo, Brazil
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Generate some hidden inputs from your array before submitting the form:
    Code:
    var f=document.forms["myForm"];
    for (var i=0; i<arr.length; i++) {
        var hidden=document.createElement("INPUT");
        hidden.type="hidden";
        hidden.name="myValues";
        hidden.value=arr[ i ];
        f.appendChild(hidden);
    }
    /Daniel

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    or use just one hidden field that contains the comma-delimited values of the array

    document.myformname.myhiddenfield.value = myArray.join();

    and then retrieve and split it up in cold fusion

  • #4
    Regular Coder
    Join Date
    Aug 2002
    Location
    São Paulo, Brazil
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by glenngv
    or use just one hidden field that contains the comma-delimited values of the array

    document.myformname.myhiddenfield.value = myArray.join();

    and then retrieve and split it up in cold fusion
    As long as you don't have commas in the values, why make things too complicated..
    /Daniel

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    You can always use a different delimiter that will not be in the values.

    myArray.join("|");
    myArray.join("~!@$");

  • #6
    New Coder
    Join Date
    Dec 2003
    Location
    Bahamas
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks guys. now just to complicate it a bit further. The array is made up of objects containing purchase data e.g.

    object.date
    object.time
    object.customerid
    object.itemcode
    object.price
    object.quantity

    this makes getting the data back on the CF side extremely difficult if I just use a join() right?

  • #7
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you're using CF 4 or higher, there are WDDX libraries which will automate the transfer of data between ColdFusion and JavaScript. Check your documentation for more on the <cfwddx ...> tag.

  • #8
    Regular Coder
    Join Date
    Jan 2004
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you are using ColdFusion, why not use ColdFusion's Array function instead of trying to do this in Javascript???

  • #9
    New Coder
    Join Date
    Dec 2003
    Location
    Bahamas
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Cant use CF for array beacuse data is creates as user enters item number for purchase by "onClick"

  • #10
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    So combine the solution of Danne and mine.

    Code:
    var f=document.forms["myForm"];
    for (var i=0; i<arr.length; i++) {
        var hidden=document.createElement("INPUT");
        hidden.type="hidden";
        hidden.name="myValues";
        hidden.value=arr[ i ].date + "," + arr[ i ].time + "," + arr[ i ].customerid + "," + arr[ i ].itemcode  + "," + arr[ i ].price  + "," + arr[ i ].quantity;
        f.appendChild(hidden);
    }
    So you have an array of hidden fields, each containing the comma-delimited purchased data.

  • #11
    New Coder
    Join Date
    Dec 2003
    Location
    Bahamas
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks. the wddx solutions appears to be a 'clean' solution.

    Glenn: What wouls be the exact code on the other side to retrieve the data? How will I know how many items are coming across? Its Monday morning and Im a but fuzzy so I just dont get it!

  • #12
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    I don't know CF but as any other server-side languages, if the fields have the same name, you retrieve it as an array. Then you split each item to form another array.

  • #13
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    In ColdFusion, fields with the same name are submitted as a comma-delimited list. You'll be just fine if you have no commas in the values in the list; that's what you'll have to watch out for, and that's why WDDX may be the best solution.

    CF has list operation functions and tags, such as ListLen(aList), ListGetAt(aList, pos), and <cfloop list="#aList#" index="item"></cfloop>, which make this kind of thing handy; however, you really have to protect against commas in your value strings.

  • #14
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Code:
    var f=document.forms["myForm"];
    for (var i=0; i<arr.length; i++) {
        var hidden=document.createElement("INPUT");
        hidden.type="hidden";
        hidden.name="myValues";
        hidden.value=arr[ i ].date + "|" + arr[ i ].time + "|" + arr[ i ].customerid + "|" + arr[ i ].itemcode  + "|" + arr[ i ].price  + "|" + arr[ i ].quantity;
        f.appendChild(hidden);
    }
    I change the delimiter from comma to pipe in each of the hidden field's value. As you can see in the type of data (date, time, customerid, itemcode, price, quantity), I don't think that there could be pipe in them. So I think that would be fine. A sample output in the server would be:

    01/14/2004|09:17 AM|123456|ABC123|1000.50|2,01/14/2004|09:17 AM|123456|ABC123|1000.50|2,01/14/2004|09:17 AM|123456|ABC123|1000.50|2

    But as Choopernickel said, WDDX may be the best solution.

  • #15
    Regular Coder
    Join Date
    Aug 2002
    Location
    São Paulo, Brazil
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    To continue on glenn's solution, you can transform all pipes and commas in the values to unicode:
    Code:
    function filter(str) {
        return new String(str).replace(
            /\,/g, escape(",")).replace(
            /\|/g, escape("|"));
    }
    
    var f=document.forms["myForm"];
    for (var i=0; i<arr.length; i++) {
        var hidden=document.createElement("INPUT");
        hidden.type="hidden";
        hidden.name="myValues";
        hidden.value=
            filter(arr[ i ].date) + "|"
            + filter(arr[ i ].time) + "|"
            + filter(arr[ i ].customerid) + "|"
            + filter(arr[ i ].itemcode)  + "|"
            + filter(arr[ i ].price)  + "|"
            + filter(arr[ i ].quantity);
        f.appendChild(hidden);
    }
    If there is a simple way to bring unicode values back, using only escape instead of filter is enough. This would change other characters into unicode as well.
    /Daniel


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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