Problem converting Coldfusion array variable to Javascript array variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kmitchell00
    New Member
    • Jun 2007
    • 4

    #1

    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:

    Code:
    <head>
    function setRepositoryLocation(formValue)
    {
         <CFWDDX action="CFML2JS" input="#repositoryArray#" toplevelvariable="jsrepositoryArray">
         alert(jsrepositoryArray.length);
         for(i=0; i<jsrepositoryArray.length; i++)
         {
    	if (jsrepositoryArray[i][0] == formValue)
    	{
                          document.frmInputSources.txtAddress1.value = jsrepositoryArray[i][1];
       	     document.frmInputSources.txtAddress2.value = jsrepositoryArray[i][2];
    	     document.frmInputSources.txtIDCountry.value = jsrepositoryArray[i][3];
    	     document.frmInputSources.txtIDState_Province.value = jsrepositoryArray[i][4];
    	     document.frmInputSources.txtCity.value = jsrepositoryArray[i][5];	     document.frmInputSources.txtPostal_Code.value = jsrepositoryArray[i][6];
    	}
         }
    }
    </script>
    </head>
    <body>
    
    <CFSET arrIndex = 1>
    <CFSET repositoryArray = ArrayNew(2)>
    <CFOUTPUT QUERY="retSourceRepositories">
    	<CFSET repositoryArray[arrIndex][1] = #idSource_Repository#>
    	<CFSET repositoryArray[arrIndex][2] = #Address1#>
    	<CFSET repositoryArray[arrIndex][3] = #Address2#>
    	<CFSET repositoryArray[arrIndex][4] = #Country#>
    	<CFSET repositoryArray[arrIndex][5] = #State_Province#>
    	<CFSET repositoryArray[arrIndex][6] = #City#>
    	<CFSET repositoryArray[arrIndex][7] = #Postal_Code#>
    	<CFSET arrIndex = #arrIndex# + 1>
    </CFOUTPUT>
    
    <select style="float:left" name="txtIDSource_Repository" id="" class=""
    	onchange="javascript:if(this.value != 0) {setRepositoryLocation(this.value);}">
    .
    .
    .
    </select>
    </body>
    Last edited by acoder; Apr 2 '12, 12:13 PM.
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Welcome to TSDN!

    This is more suitable for the Coldfusion forum to which I've moved this thread.

    Comment

    • improvcornartist
      Recognized Expert Contributor
      • May 2007
      • 303

      #3
      The first thing I notice in your code is that you have an </script> tag but no beginning script tag.

      Comment

      • kmitchell00
        New Member
        • Jun 2007
        • 4

        #4
        Oops. Copy-and-paste mistake. Here's the beginning <script> tag:

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

        Thanks for pointing that out.

        Comment

        • improvcornartist
          Recognized Expert Contributor
          • May 2007
          • 303

          #5
          I'm not familiar with CFWDDX, so I probably can't be of much help. Does the CF repositoryArray fill correctly? Does jsrepositoryArr ay need to be declared before the CFWDDX tag?

          Comment

          • kmitchell00
            New Member
            • Jun 2007
            • 4

            #6
            The CF repositoryArray is populating correctly. Regarding declaring jsrepositoryArr ay 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 jsrepositoryArr ay first and it still doesn't work.

            Comment

            • improvcornartist
              Recognized Expert Contributor
              • May 2007
              • 303

              #7
              One thing to try may be adding a src to the javascript tag:

              Code:
              <script type="text/javascript" language="javascript" src="/CFIDE/scripts/wddx.js">
              </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.

              Comment

              • kmitchell00
                New Member
                • Jun 2007
                • 4

                #8
                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 :

                Code:
                <SCRIPT LANGUAGE="javascript" TYPE="text/javascript">
                
                <CFWDDX action="CFML2JS" input="#repositoryArray#" toplevelvariable="jsrepositoryArray" output="jsArray">
                
                <CFOUTPUT>#jsArray#</CFOUTPUT>
                
                </SCRIPT>
                Thanks for all the suggestions.
                Last edited by acoder; Apr 2 '12, 12:13 PM.

                Comment

                Working...