Skip to main content
Participant
May 15, 2009
Question

CFloop in Javascript function...pls help urgent

  • May 15, 2009
  • 1 reply
  • 1208 views

<script type="text/Javascript">
      function XYZ(group_id)
      {
       switch(group_id)
       {
        <cfloop query = "GetProgramAuthGrpRouting">
         case '#GetProgramAuthGrpRouting.group_id#':
          <cfset variables.user_names = "">
          <!--- Get all users belonging to the selected group id --->
          <cfquery name="GetGroupUsers" datasource="#request.db_instance#">
           select distinct employee_number from dbo.DIV_USER_GROUP_XREF where division =
            <cfqueryPARAM value = "#request.Pub.Project.division#"> and group_id =
            <cfqueryPARAM value = "#GetProgramAuthGrpRouting.group_id#">
          </cfquery>
          <!--- Getting the name of the user by passing the employee id to the GetUser custom tag  --->
          <cfif GetGroupUsers.RecordCount gt 0>
           <cfloop query = "GetGroupUsers">
            <CF_GetUser
             query_name = "GetUser"
             employee_number = "#GetGroupUsers.employee_number#">
            <cfset variables.user_names = variables.user_names&''&GetUser.employee_name&'\n'>
           </cfloop>
          <cfelse>
           <cfset variables.user_names = 'No users in this routing group\n'>
          </cfif>
            alert('#variables.user_names#');
            break;
          </cfloop>
       }
      }
     </script>

Hi all I am new to this community and this is my first post here. So if this is the wrong place for these then pls let me know the right area. Thanks.I have looped over the result set using CFLOOP in  javascript function and it is working fine. But I want to know if this can be done since javascript is a client side scripting language and CF is a server side. Is this the right way of coding? Can i use cfloop in a javascript function? Please help me as this is urgent. Thanks in advance.

    This topic has been closed for replies.

    1 reply

    ilssac
    Inspiring
    May 15, 2009

    You you can use CFML inside of a JavaScript <script..> block to dynamically build the JavaScript code to send to the client.  It is a very powerful technique for building highly interactive web sites.

    Just realize that all the JavaScript code is going to be completely built by the CFML and then sent to the browser client through the web server where the JavaScript is executed completely independent of anything that is running on the server.  The JavaScript can not read or write variables on the server or vice-a-versa.  But it is quite easy to export variable values from ColdFusion to JavaScript.  It is a bit more work to export variable values back requiring either the browser or JavaScript to make a new request of the server to send the data.  AJAX does allow this to happen behind the scenes making it look like the data is being shared.  But in reality it is being passed back and forth with independent HTTP requests and responses.

    A recent discussion on this topic:

    http://forums.adobe.com/message/1957350#1957350