Accessing CF array data from withing JavaScript code

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default Accessing CF array data from withing JavaScript code

    Good afternoon!

    I'm having a problem where there is probably a very simple solution, but I'm
    up against a very rigid time schedule, and I could use some help resolving this
    issue. (I'm not a JavaScript expert for one thing!)

    I have an application that consists of CF (ver 5) and imbedded Javascript for
    form/data validation; dynamic loading of data in forms, etc. I have a CF query
    that selects data from a MS SQL table that is needed to fill a number of fields
    in a form that is created in JavaScript. I can create a CF array and get the
    data I need loaded in the array (single dimension) just fine. The code below
    shows the CF code and a sample of the data in the array. What I need to be able
    to do is then access the data from within JavaScript code so that I can place
    the individual elements in the form. I know that there is something very basic
    that I'm missing here, but with all the other aspects of the application that
    are consuming me, I can't seem to focus on the solution! Any help would, of
    course, be greatly appreciated.

    The CF code:

    ************************************************** *****
    ************************************************** *****
    <cfquery name="test2x" dbtype="query">
    SELECT contract,
    job_cd,
    st_hr,
    stlabor,
    fy,
    fymon,
    mon,
    mon3
    FROM test1x
    WHERE mon3 = 'xxx'
    ORDER BY contract,
    job_cd,
    fy
    </cfquery>

    <cfset CstArray1 = ArrayNew(1)>
    <cfset Res1 = ArrayClear(CstArray1)>

    <cfif test2x.recordcount>
    <cfoutput query="test2x">
    <cfset ThisItem=ArrayLen(CstArray1) +1>
    <cfset CstArray1[ThisItem]=test2x.stlabor>
    </cfoutput>
    </cfif>

    <cfloop index = "Order" from = "1" to = "#ArrayLen(CstArray1)#">
    <cfoutput>CstArray1[#Order#] value is #CstArray1[Order]#</cfoutput><br>
    </cfloop>
    ************************************************** *******
    ************************************************** *******

    And a printout of the data:

    ************************************************** *******
    ************************************************** *******
    CstArray1[1] value is 291.00
    CstArray1[2] value is 3819.00
    CstArray1[3] value is 754.00
    CstArray1[4] value is 221.00
    CstArray1[5] value is 4988.00
    CstArray1[6] value is 3121.00
    CstArray1[7] value is 2020.00
    CstArray1[8] value is 3356.00
    CstArray1[9] value is 14465.00
    ************************************************** *******
    ************************************************** *******

    This is the correct data (in this example) that needs to be loaded in the
    JavaScript form.

    Thanks!
    Bill
    [email]lohneb@bellsouth.net[/email]



    LohneB Guest

  2. Similar Questions and Discussions

    1. Accessing swf or javascript in different html
      Hi, From my understanding, the swf instance can communicate with other swf instance through the LocalConnection class. Also, the flex application...
    2. How to view the code of a Javascript array
      <script language="javascript" src="http://www.thefreedictionary.com/_/WoD/jsa.aspx"></script> sends me a Javascript array. How do I view the code...
    3. Accessing data from C# code-behind in the ItemDataBound Event
      Hello: If I want to access a particular column of data that is bound to a datagrid from within the ItemDataBound Event, how would I do that? I...
    4. Accessing Dataset data from code ... how?
      Hi experts! here's a xml example of what i've got in my DataSet : <year>2002</year> <directors> <director> <first_name>Faouzi</first_name>...
    5. Accessing elements in array ref of array references
      Currently I'm comparing the first value of each of the array references (which are stored in an array reference $ref_ref) as follows: ...
  3. #2

    Default Re: Accessing CF array data from withing JavaScript code

    Hi Bill,

    You have your value return just fine. Now you need to translate the output
    into JS variable assignments.

    Just for grins:

    <cfset CstArray1 = ArrayNew(1)>

    <cfset CstArray1[1]=291.00>
    <cfset CstArray1[2]=3819.00>
    <cfset CstArray1[3]=754.00>
    <cfset CstArray1[4]=221.00>
    <cfset CstArray1[5]=4988.00>
    <cfset CstArray1[6]=3121.00>
    <cfset CstArray1[7]=2020.00>
    <cfset CstArray1[8]=3356.00>
    <cfset CstArray1[9]=14655.00>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

    <html>
    <head>
    <title>Untitled</title>


    <script language="Javascript1.2">
    function fillval() {

    <cfoutput>
    var CstArray1 = new Array();
    <cfloop index="thisval" from="1" to = "#evaluate(arraylen(CstArray1)-1)#">
    CstArray1[#thisval#] = #CstArray1[thisval]#;
    </cfloop>
    </cfoutput>
    alert(CstArray1[3]);
    }

    </script>
    </head>

    <body onload="fillval();">



    </body>
    </html>

    HTH,

    philh Guest

  4. #3

    Default Re: Accessing CF array data from withing JavaScript code

    If you are using cf7, use the toscript function.
    Dan Bracuk Guest

  5. #4

    Default Re: Accessing CF array data from withing JavaScript code

    For reference, I'm using CF 5 - I should have included that information originally. Sorry about that.
    LohneB Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139