Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing Cold Fusion query data from within Javascript

Status
Not open for further replies.

LohneB

Programmer
May 4, 2000
84
US
Good morning!

I have posted this on the Macromedia Cold Fusion forum also.

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 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 SQL table that is needed to fill a number of fields in a form that created in Javascript. I can create a CF array and get the data needed 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 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

Bill
lohneb@bellsouth.net

 
I've never used cold fusion so my syntax may be way off, but try something like this:
Code:
<script language="javascript">
var blah = new Array();
<cfloop index = "Order" from = "1" to = "#ArrayLen(CstArray1)#">
    <cfoutput>
       blah[#Order#] = #CstArray1[Order]#;[i][COLOR=grey](put the CF character for a line break here)[/color][/i]
   </cfoutput>
</cfloop>
</javascript>
If that works the way I think it should your output should look something similar to this:
Code:
<script language="javascript"
var blah = new Array();
blah[1] = 291.00;
blah[2] = 3819.00;
blah[3] = 754.00;
blah[4] = 221.00;
blah[5] = 4988.00;
blah[6] = 3121.00;
blah[7] = 2020.00;
blah[8] = 3356.00;
blah[9] = 14465.00;
</script>
Which is perfectly valid javascript and will make all your values accessible in any javascript function.

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
Thanks -kaht.

I think I did what you suggested with the resulting error message:

************************************************************
Error Occurred While Processing Request
Error Diagnostic Information

An error occurred while evaluating the expression:


#CA1[Order]#



Error near line 116, column 32.
--------------------------------------------------------------------------------

Error resolving parameter CA1


ColdFusion was unable to determine the value of the parameter. This problem is very likely due to the fact that either:

You have misspelled the parameter name, or
You have not specified a QUERY attribute for a CFOUTPUT, CFMAIL, or CFTABLE tag.


The error occurred while processing an element with a general identifier of (#CA1[Order]#), occupying document position (116:28) to (116:39) in the template file d:\inetpub\************************************************************

The code that generated the error is as follows:

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

<cfif test2x.recordcount>
<cfoutput query="test2x">
<cfset ThisItem=ArrayLen(CstArray1) +1>
<cfset CstArray1[ThisItem]=test2x.stlabor_l>
</cfoutput>
</cfif>
<br>
<cfloop index = "Order" from = "1" to = "#ArrayLen(CstArray1)#">
<cfoutput>CstArray1[#Order#] value is #CstArray1[Order]#</cfoutput><br>
</cfloop>
<br>
<br>

<script language="Javascript">
var CA1 = new Array();
<cfif test2x.recordcount>
<cfloop index = "Order" from = "1" to = "#ArrayLen(CstArray1)#">
<cfoutput>
CA1[#Order#] = #CstArray1[Order]#;<br>
</cfoutput>
</cfloop>
</cfif>
</script>
<script language="Javascript">
var CA1 = new Array();
<cfloop index = "Order" from = "1" to = "#ArrayLen(CstArray1)#">
<cfoutput>
CA1[#Order#]JS value is #CA1[Order]#
</cfoutput><br>
</cfloop>
</script>
************************************************************************************************************************

The array contains the following data as shown from the first <cfloop>. (This just shows that the array contains data that should be copied into the new array within javascript as you suggest.)

Close - I know - but not quite there. Any ideas??

Thanks.


Bill
lohneb@bellsouth.net
 
Any ideas??
Yeah, since you're getting a cold fusion error perhaps you should ask in that forum. Sorry, I have 0% CF experience.


-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
Actually, I have done that. The CF guys originally said about the same thing that you did, so great minds think alike! At any rate, the problem is still being worked. Thanks for your help.

Bill
lohneb@bellsouth.net

 
Thanks - issue resolved to the point where I can finish debugging what I have. Again, thank you.

Bill

Bill
lohneb@bellsouth.net

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top