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 CF table data within Javascript

Status
Not open for further replies.

LohneB

Programmer
May 4, 2000
84
US
Good morning!

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

 
the basic thing you're missing is that CF is server side and javascript is client side. by the time the client gets the page your array has come and gone. It means absolutely nothing to the browser so JS can not directly use CF arrays.

however you're half way there.

<cfloop index = "Order" from = "1" to = "#ArrayLen(CstArray1)#">
<cfoutput>CstArray1[#Order#] value is #CstArray1[Order]#</cfoutput><br>
</cfloop>
if you were to put this into a javascript block and put the values into JS you'll be able to get to the values.

this may be a little off its been a while since i've worked with JS arrays
Code:
<script language = "javascript">
var yourJSarray= new Array(0);
<cfoutput>
<cfloop index = "Order" from = "1" to = "#ArrayLen(CstArray1)#">
yourJSarray[#order#] = #CstArray1[Order]#;
</cfloop>
</cfoutput>
</script>

We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true.
 
Thanks 'TruthInSatire'!

I think I did what you suggested - the code follows - I get an error message as follows:

Error Occurred While Processing Request
Error Diagnostic Information

An error occurred while evaluating the expression:


#JSarray[Order]#


************************************************************
Error near line 95, column 35.
------------------------------------------------------------

Error resolving parameter JSARRAY


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 (#JSarray[Order]#), occupying document position (95:27) to (95:42) in the template file d:\inetpub\
************************************************************

The code that created this error is:

************************************************************************************************************************
<html>
<!--- Gets pre current year data --->
<cfquery name="test1x" datasource="MIDAS">
SELECT f.contract_id,
f.prjid,
f.rev,
f.ver,
f.fy,
f.fymon,
f.mon,
f.mon3,
f.plc_cd,
f.st_hc,
f.st_hr,
f.stlabor_l,
f.matmaint_l,
f.ma_cost,
f.subc_ga,
f.odc_ga,
f.award_fee,
f.incen_fee,
f.baseline,
f.cc4,
e.lname
FROM FORECAST_SUB f,
EMPS e
WHERE f.contract_id = 1
AND f.prjid='99'
AND f.rev='1'
AND f.ver='S'
AND f.active = 'Y'
AND f.baseline = 'N'
AND f.cnt_yr = '3'
AND f.mon3 = 'xxx'
AND f.plc_cd = e.emp_id
AND f.fy < '2005'
ORDER BY f.contract_id,
f.plc_cd,
fymon,
fy
</cfquery>

<!--- Gets the data needed for the FORECAST_SUB table for labor costs --->
<cfquery name="test2x" dbtype="query">
SELECT contract_id,
plc_cd,
st_hr,
stlabor_l,
fy,
fymon,
mon,
mon3
FROM test1x
WHERE mon3 = 'xxx'
ORDER BY contract_id,
plc_cd,
fy
</cfquery>
<cfset test2cnt = test2x.recordcount>


<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 JSarray = new Array();
<cfoutput>
<cfif test2x.recordcount>
<cfloop index = "Order" from = "1" to = "#ArrayLen(CstArray1)#">
JSarray[#Order#] = #CstArray1[Order]#;<br>
</cfloop>
</cfif>
</cfoutput>
</script>

<script language="JavaScript">
<cfoutput>
<cfloop index = "Order" from = "1" to = "#ArrayLen(CstArray1)#">
CA1[#Order#]JS value is #JSarray[Order]#
</cfloop>
</cfoutput>
</script>



<head>
<title>test2</title>



</head>

<body>

</body>
</html>
************************************************************************************************************************

??? Any suggestions ???

Thanks again!

Bill
lohneb@bellsouth.net
 
<script language="JavaScript">
<cfoutput>
<cfloop index = "Order" from = "1" to = "#ArrayLen(CstArray1)#">
CA1[#Order#]JS value is #JSarray[Order]#
</cfloop>
</cfoutput>
</script>
its in that loop. what is it for? you have it in JS script tag but it isn't JS.

We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true.
 
It is just there to list the array elements. Sorry I didn't explain that. Not present it the 'real' application code.

Bill


 
well take it out. its broken

We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true.
 
JSArray is not a CF structure; it lives on the client side, so you can't use it in CF macro replacement areas.

Try
<script language="JavaScript">
<cfoutput>
<cfloop index = "Order" from = "1" to = "#ArrayLen(CstArray1)#">
CA1[#Order#]JS value is JSarray[#Order#]
</cfloop>
</cfoutput>
</script>

this would work, but I'm not entirely sure that's what you're after.

Phil Hegedusich
Senior Programmer/Analyst
IIMAK
-----------
I'm not as think as you confused I am.
-----------
Flabbergasted (a.): Amazed at how much weight one has gained.
-----------
Oyster (n.): One who sprinkles their conversation with Yiddish expressions.
 
I think he's just trouble shooting.

We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true.
 
philhege

Question - isn't what you show the same as I had for that part? Looks the same to me.

The part I'm trying to get to work is

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

where I end up with a JS array (JSarray) that can then be used with a line of code like

labor000 = <cfoutput>#JSarray[Order]#</cfoutput>

and 'labor000' is the value for a form field. Right now, I get an error saying that the #JSarray[Order]# cna't be evaluated because the parameter JSARRAY can'b be resolved. Error says CF is unable to determine the value of the parameter.

Does this help explain my problem?

Bill
 
again, you're trying to use CF and JS interchangeably. not possible. You created a JS array to be used by the client. now you're trying to use it by CF. not going to happen.

You have to use JS to set the value of labor000 if you're going to use the JS array.

what is it you're trying to do. it doesn't sound like you need JS if you're just trying to set a form field to a value you can do this

<input type = "text" name = "labor000" value = "<cfoutput>#someValue#</cfoutput>">

We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true.
 
TruthInSatire,

Let me see if I can come up with a better explanation of what I'm trying to do. Maybe I over-simplified my original explanation of what has to be done within the application. It will take me some time to put this together - I will add it to this thread when I get done (may not be today.)

Again, thanks for working with me on this.

Bill
 
Thanks to the help received here, the application now works to the point where I can debug the rest. Again, thanks!

Bill

Bill
lohneb@bellsouth.net

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top