471,594 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 software developers and data experts.

Problem converting Coldfusion array variable to Javascript array variable

I am coding in Coldfusion MX7 and using Javascript for some of the functionality. The basic functionality I'm coding is, based on a value the user chooses from a dropdown box, I populate address fields with the associated data. I am using the onChange attribute of the dropdown box for the call to a Javascript function. I prepopulate a Coldfusion array with the address data and am using cfwddx to convert the Coldfusion array to a Javascript array. My problem is that the conversion doesn't seem to be working. The length of the Javascript array is 0, when it should be 3. Any suggestions?

Here's the relevant code:

Expand|Select|Wrap|Line Numbers
  1. <head>
  2. function setRepositoryLocation(formValue)
  3. {
  4.      <CFWDDX action="CFML2JS" input="#repositoryArray#" toplevelvariable="jsrepositoryArray">
  5.      alert(jsrepositoryArray.length);
  6.      for(i=0; i<jsrepositoryArray.length; i++)
  7.      {
  8.     if (jsrepositoryArray[i][0] == formValue)
  9.     {
  10.                       document.frmInputSources.txtAddress1.value = jsrepositoryArray[i][1];
  11.             document.frmInputSources.txtAddress2.value = jsrepositoryArray[i][2];
  12.          document.frmInputSources.txtIDCountry.value = jsrepositoryArray[i][3];
  13.          document.frmInputSources.txtIDState_Province.value = jsrepositoryArray[i][4];
  14.          document.frmInputSources.txtCity.value = jsrepositoryArray[i][5];         document.frmInputSources.txtPostal_Code.value = jsrepositoryArray[i][6];
  15.     }
  16.      }
  17. }
  18. </script>
  19. </head>
  20. <body>
  22. <CFSET arrIndex = 1>
  23. <CFSET repositoryArray = ArrayNew(2)>
  24. <CFOUTPUT QUERY="retSourceRepositories">
  25.     <CFSET repositoryArray[arrIndex][1] = #idSource_Repository#>
  26.     <CFSET repositoryArray[arrIndex][2] = #Address1#>
  27.     <CFSET repositoryArray[arrIndex][3] = #Address2#>
  28.     <CFSET repositoryArray[arrIndex][4] = #Country#>
  29.     <CFSET repositoryArray[arrIndex][5] = #State_Province#>
  30.     <CFSET repositoryArray[arrIndex][6] = #City#>
  31.     <CFSET repositoryArray[arrIndex][7] = #Postal_Code#>
  32.     <CFSET arrIndex = #arrIndex# + 1>
  33. </CFOUTPUT>
  35. <select style="float:left" name="txtIDSource_Repository" id="" class=""
  36.     onchange="javascript:if(this.value != 0) {setRepositoryLocation(this.value);}">
  37. .
  38. .
  39. .
  40. </select>
  41. </body>
Jun 17 '07 #1
7 8670
16,027 Expert Mod 8TB
Welcome to TSDN!

This is more suitable for the Coldfusion forum to which I've moved this thread.
Jun 18 '07 #2
303 Expert 100+
The first thing I notice in your code is that you have an </script> tag but no beginning script tag.
Jun 18 '07 #3
Oops. Copy-and-paste mistake. Here's the beginning <script> tag:

<script language="javascript" type="text/javascript">

Thanks for pointing that out.
Jun 18 '07 #4
303 Expert 100+
I'm not familiar with CFWDDX, so I probably can't be of much help. Does the CF repositoryArray fill correctly? Does jsrepositoryArray need to be declared before the CFWDDX tag?
Jun 18 '07 #5
The CF repositoryArray is populating correctly. Regarding declaring jsrepositoryArray prior to the CFWDDX statement, The examples I've seen of CFWDDX didn't show that prior declaring of variables was required. I tried declaring the variable jsrepositoryArray first and it still doesn't work.
Jun 19 '07 #6
303 Expert 100+
One thing to try may be adding a src to the javascript tag:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript" language="javascript" src="/CFIDE/scripts/wddx.js">
  2. </script>
The wddx.js file might be required for the cfwddx tag. If that is no help, you might be able to use the output attribute with the cfwddx tag, which should be able to tell you the output of the operation.
Jun 19 '07 #7
Thanks for the suggestion. I did add that line. I also found my other problem. I shouldn't have put the <cfwddx tag within the javascript, but after i populated the coldfusion array.

Here are the code lines I put after populating the repositoryArray:

Expand|Select|Wrap|Line Numbers
  1. <SCRIPT LANGUAGE="javascript" TYPE="text/javascript">
  3. <CFWDDX action="CFML2JS" input="#repositoryArray#" toplevelvariable="jsrepositoryArray" output="jsArray">
  5. <CFOUTPUT>#jsArray#</CFOUTPUT>
  7. </SCRIPT>
Thanks for all the suggestions.
Jun 24 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

39 posts views Thread by Martin JÝrgensen | last post: by
7 posts views Thread by Trickynick1001 | last post: by
2 posts views Thread by am_pcguy | last post: by
2 posts views Thread by sorobor | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.