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!

How To Reference Cold Fusion Array Values in JS?

Status
Not open for further replies.

scripter73

Programmer
Apr 18, 2001
421
US
Hi,

I have a Cold Fusion query that populates a Cold Fusion array. Now I want to display the query result in a Javascript table for further processing. I was hoping to do this by building another Javascript table based on the values.

I haven't been very successful in creating an array in Javascript to hold the query result.

What am I doing wrong? I'm getting Javascript errors concerning an 'invalid character' on an empty line, and also there's no object.

Following is my code:
Ideally I want to read the values of 'myarray' in Javascript


<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>

<html>
<head>
<title>Build Dynamic Array From VehicleMk</title>
<!--- <CFQUERY NAME = &quot;VEHCODE&quot; DATASOURCE = &quot;CCMSLRDTA&quot;> --->
<CFQUERY NAME = &quot;VEHCODE&quot; DATASOURCE = &quot;CCMS&quot;>
SELECT CODE,DESCRIPTION FROM TBLVEHICLE
</CFQUERY>


<CFLOOP QUERY = &quot;VEHCODE&quot;>
<CFSET BOTHFIELDS = LISTAPPEND(VEHCODE.CODE[CurrentRow],VEHCODE.DESCRIPTION[CurrentRow])>
<CFSET myarray[CurrentRow][1] = BOTHFIELDS>
<!--- As loop through array, compare the Description to your selected vehicle. If
there's a match, #session.dbcarmake# = Vehcode.Code of Current Row. --->
</CFLOOP>



<script langage=&quot;javascript&quot;>
function receive_arr(){

var n=new Array(#vehcode.Recordcount#)
<cfloop query=&quot;vehcode&quot;>
n[#CurrentRow#]=&quot;#vehcode.code[CurrentRow]#&quot;
</cfloop>

for (i=1; i<6; i++){
alert(n);
}//for
}//function
</script>

</head>

<body>
<form>


<SELECT name=&quot;car_maker&quot;>
<option value=&quot;#&quot; selected></option>
<CFOUTPUT query=&quot;carmake&quot;>
<OPTION value=&quot;#carmake.vehicle_make_desc#&quot;>#carmake.vehicle_make_desc#
</CFOUTPUT>
</SELECT>
<input type=&quot;button&quot; name=&quot;Pass Array&quot; value=&quot;Pass Array&quot; onclick=&quot;receive_arr()&quot;;>
</form>
</body>
</html>



Any help you can provide is greatly appreciated.

Thanks in advance,
scripter73
 
Ok, I looked at it some more, and I got this much code to work. However, now that I've created an array in Javascript, I want to print out the contents to verify the values. When I attempt to print the array from another Javascript function, I get 'undefined' for my values. When I try to print the values from within the update_my_array() function, I don't receive anything. Is there a way to verify the Javascript array just with a print out?


<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>

<html>
<head>
<title>Build Dynamic Array From VehicleMk</title>


<script langage=&quot;javascript&quot;>
var codes = new Array();

function update_my_array(value,index){
var str
codes[index] = value;
str = codes[index];
document.write(str, &quot;<p>&quot;);
}//function


function show_array(count){
for (i = 0; i<count; i++)
document.write(codes + &quot;<br>&quot;);
}
</script>

<CFQUERY NAME = &quot;VEHCODE&quot; DATASOURCE = &quot;CCMS&quot;>
SELECT CODE,DESCRIPTION FROM TBLVEHICLE
</CFQUERY>


<CFOUTPUT QUERY = &quot;VEHCODE&quot;>
<CFSET BOTHFIELDS = LISTAPPEND(VEHCODE.CODE[CurrentRow],VEHCODE.DESCRIPTION[CurrentRow])>
update_my_array('#BOTHFIELDS#', #CURRENTROW#);<br>

</CFOUTPUT>


<input type=&quot;button&quot; name=&quot;showjs&quot; value=&quot;Show JS array&quot; onclick=&quot;show_array(30)&quot;;>


</head>

<body>
<form>

</body>
</html>



Thanks for the help,
scripter73
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top