How to send form data to a cfc using createObject in ColdFusion
Today I am going to show you how to post form data to a ColdFusion cfc. I will not go into form validation. This is simply an explaination of the use of createObject.
createObject vs <cfobject>
First off, the difference between createObject and is syntax. createObject is a function, where is a ColdFusion tag. In other words, you would use createObject in a tag like cfset or cfscript.
mycfc.cfc
<cfcomponent output="false"> <cffunction name="myfunction" output="false" access="public" returntype="void"> <cfargument name="firstname" required="true" /> </cffunction> </cfcomponent>
Now we have a cfc called ‘mycfc’ in a folder called ‘cfcFolder’. Below is the code inside mycfc.
Now we also have a ColdFusion .cfm page called myform.cfm that contains a form that needs to use the above cfc. Below is our code.
Note that I have removed all HTML tags for understanding how this process works.
myform.cfm
<cfform action="#CGI.SCRIPT_NAME#"> <cfinput type="text" name="firstname" /> <cfinput type="submit" name="myformbutton" value="call my name"/> </cfform>
Now we can use form or cfform. The above form is a text box called firstname and a button called myformbutton. If you add a some text in the text box and click the button, it will reload the page with the form variables defined. In other words, when the button is pressed and the pages is reloaded, the form will “process” the data in the form text box and turn it into a variable called “form.firstname”.
So now we have to form creating the variables and is defined. Now we want to send our data to our cfc. To be inserted in to a database or do other things.
When the page is reloading gives us perfect time to send our data, but there is a catch. We load our page when the button is not pressed too. So we need to check to see if the form variables exists and if they do then send data off to the cfc.
<cfif IsDefined("form.myformbutton")> ... </cfif>
Now I could have just said if the form is defined, but what if there are multiple forms of the page. We will need something unique. Well, we can use the button, or we could simple name the form something different. I choose the button. The above cfif statement will check if the form variables are defined.
How to use createObject
Now we need to create our object.
This creates an instance of the object and stores this in a variable called
“comVar” that can be utilized in further code to repeatedly access
methods of the object as such: comVar.myfunction() .
<cfset comVar= createObject("component", "cfcFolder.mycfc") />Lets set our form data to the object
<cfset comVar.firstname = "#form.firstname#">Finally lets get rid of it and send it off to the cfc. This is where you can access your methods in your cfc. Make sure you can access your method by setting your ‘access’ attribute on your cffunction.
<cfset comVar.myfunction()>Lets review
Today we learned how to send form data from your cfm page to your cfc using createObject. The full code for myform.cfm is posted below for more understanding of how the process works.
myform.cfm – full code
<cfif IsDefined("form.myformbutton")> <cfset comVar= createObject("component", "cfcFolder.mycfc")/> <cfset comVar.firstname = "#form.firstname#"> <cfset comVar.myfunction()> </cfif> <cfform action="#CGI.SCRIPT_NAME#"> <cfinput type="text" name="firstname" /> <cfinput type="submit" name="myformbutton" value="call my name"/> </cfform>