2 Replies Latest reply on Dec 25, 2012 7:10 AM by Dan Bracuk

    Is it possible to return a database query without refreshing the page?

    solaced Level 1

      I have a form which has a drop-down select box. What I want to do is send the value from the select list to a CFC which runs some SQL code and returns a recordset. I then want to use this recordset on my original CFM page that has the form on it.

       

      Is it possible to do this without refreshing the page through some kind of AJAX? Apologies I am a newbie at AJAX but I did manage to write a bit of code that uses <cfdiv> to bind to a URL that does the database work and returns some HTML. But really I just want the database recordset and not sure how to get it?

        • 1. Re: Is it possible to return a database query without refreshing the page?
          BKBK Adobe Community Professional & MVP

          You could use AJAX, Flex Remoting or any such remoting technique. That is, if there is a case for it. However, from what you describe in the first paragraph, you don't need to. Just send the form to its own page.

           

          In the following example, the CFC and CFM page are both in the same directory under the root. I have made use of the cfdocexamples datasource which ships with ColdFusion.

           

          Employee.cfc

          <cfcomponent>

          <cffunction name="getEmployeeDetails"  output="false" returntype="query">

          <cfargument name="employee_id">

          <cfset var employeeDetails = queryNew("","")>

          <cfset var emp_id = arguments.employee_id>

           

          <cfif isNumeric(emp_id)>

              <cfquery name = "employeeDetails" dataSource = "cfdocexamples">

                  SELECT firstname, email as email_name, department, phone, location

                  FROM Employees

                  WHERE emp_id = <cfqueryparam cfsqltype="cf_sql_integer" value="#emp_id#">

              </cfquery>

          </cfif>

           

          <!--- Will activate error-handler in the caller --->

          <cfif NOT isNumeric(emp_id) or employeeDetails.recordcount EQ 0>

              <cfthrow message="You selected no employee.">

          </cfif>

           

          <cfreturn employeeDetails>

          </cffunction>

          </cfcomponent>

           

          testPage.cfm

          <cfif isDefined("form.employee_id")>

              <cfset employeeObj = createobject("component","Employee")>

              <!--- employeeDetails is a query--->

              <cfset employeeDetails = employeeObj.getEmployeeDetails(form.employee_id)>

              <cfdump var="#employeeDetails#">

          </cfif>

           

          <cfform action="#CGI.SCRIPT_NAME#">

          <cfselect name="employee_id">

          <option value="0">Employee</option>

          <option value="1">Carolynn Petersen</option>

          <option value="2">Dave Heartside</option>

          <option value="3">Linda Stewart</option>

          </cfselect>

          <cfinput name="sbmt" type="submit" value="Send">

          </cfform>

          • 2. Re: Is it possible to return a database query without refreshing the page?
            Dan Bracuk Level 5

            The easiest way to do this is by binding your cfform elements to your cfc.  The documentation on cfinput will get you started.