To invoke a method using a ColdFusion or HTML form, the following must be true:
form
or cfform tag action
attribute must specify the CFC filename or path followed by ?method=
methodname, where methodname is the name of the method; for example, <form action="myComponent.cfc?method=myMethod" method="POST">.
name
attribute of the tag must be the method parameter name and the field value is the parameter value.
access="remote"
attribute.
If the CFC method that you invoke from the form displays output directly, the user's browser shows the output. (You can use the cffunction tag output
attribute to disable displaying output.) If the CFC returns a result using the cfreturn tag, ColdFusion converts the text to HTML edit format, puts it in a WDDX packet, and includes the packet in the HTML that it returns to the client.
<h2>Find People</h2> <form action="components/corpQuery.cfc?method="getEmp" method="post"> <p>Enter employee's last Name:</p> <input type="Text" name="lastName"> <input type="Hidden" name="method" value="getEmp"> <input type="Submit" title="Submit Query"><br> </form>
In the example, the form
tag's action
attribute points to the corpQuery
component and invokes the getEmp
method.
access="remote"
for each cffunction
tag, as the following example shows:
<cfcomponent> <cffunction name="getEmp" access="remote"> <cfargument name="lastName" required="true"> <cfset var empQuery=""> <cfquery name="empQuery" datasource="cfdocexamples"> SELECT LASTNAME, FIRSTNAME, EMAIL FROM tblEmployees WHERE LASTNAME LIKE '#arguments.lastName#' </cfquery> <cfoutput>Results filtered by #arguments.lastName#:</cfoutput><br> <cfdump var=#empQuery#> </cffunction> </cfcomponent>
http://localhost/corpFind.cfm
ColdFusion displays the search form. After you enter values and click the Submit Query button, the browser displays the results.