Add a database record with Flex 3 & Coldfusion
Continuing my work towards a very simple Flex 3/Coldfusion CRUD tutorial, I’ve completed my installment covering adding a new record to a database. Lets go:
For starters, I am using the same Coldfusion Component I have used in all of my examples. This time I have added a method called InsertEmployee to insert new records. The complete CFC now looks like this:
<cfcomponent>
<cfset dataSrc = “mydatasource”><cffunction name=”InsertEmployee” access=”remote” returntype=”void” output=”no”>
<cfargument name=”firstName” required=”yes” type=”string”>
<cfargument name=”lastName” required=”yes” type=”string”>
<cfargument name=”officePhone” required=”yes” type=”string”>
<cfquery name=”addEmp” datasource=”#dataSrc#”>
INSERT INTO employees (firstName, lastName, officePhone)
VALUES (‘#firstName#’,'#lastName#’,'#officePhone#’)
</cfquery>
</cffunction><cffunction name=”FindAll” access=”remote” returntype=”xml” output=”no”>
<cfquery name=”getEmp” datasource=”#dataSrc#”>
SELECT firstName, lastName, officePhone, id
FROM employees
</cfquery>
<!— Convert Query to xml —>
<cfprocessingdirective suppresswhitespace=”Yes”>
<cfcontent type=”text/xml; charset=utf-8″>
<cfxml variable=”xmlobject”> �
<response>
<data>
<cfoutput query=”getEmp”>
<row>
<id>#id#</id>
<firstName>#firstName#</firstName>
<lastName>#lastName#</lastName>
<officePhone>#officePhone#</officePhone>
</row>
</cfoutput>
</data>
</response>
</cfxml>
<!— Convert back to a string —>
<cfset myvar=toString(xmlobject)>
</cfprocessingdirective>
<cfreturn myvar>
</cffunction>
</cfcomponent>
Now we just need our MXML application. I’ve created a very simple form, nothing more. It beings with our opening <mx:Application> tag followed by the actual form:
<mx:Form width=”279” x=”24” y=”24“>
<mx:HBox horizontalGap=”8“>
<mx:Label text=”First Name “/>
<mx:TextInput id=”firstName“/>
</mx:HBox>
<mx:HBox>
<mx:Label text=”Last Name “/>
<mx:TextInput id=”lastName“/>
</mx:HBox>
<mx:HBox>
<mx:Label text=”Office Phone“/>
<mx:TextInput id=”officePhone“/>
</mx:HBox>
<mx:Button label=”Submit” click=”clickHandler()”/>
</mx:Form>
Nothing special here, I’ve placed each Label and Textinput inside their own HBox only because that is now I saw it done someplace else. Layout really means nothing for this example so lay it out however you want, you really only need the start and ending <mx:Form> tags and the <mx:TextInput> tags.
Next we call our web service which is our Coldfusion CFC listed at the top of the article:
<mx:WebService id=”userRequest” wsdl=”http://brad.melendy.com/projects/webservice/employees.cfc?wsdl“>
<mx:operation name=”InsertEmployee” fault=”mx.controls.Alert.show(event.fault.faultString)”/>
</mx:WebService>
Notice that in this case, rather than passing the method name in the URL with the <mx:HTTPService> tag, I’m using the <mx:WebService> tag, passing the web service in WSDL format and telling it the method to execute in the <mx:operation> tag.
The last step is our ActionScript:
<mx:Script>
<![CDATA[
import mx.controls.Alert;
private function clickHandler():void {
userRequest.InsertEmployee(firstName.text, lastName.text, officePhone.text);
firstName.text = "";
lastName.text = "";
officePhone.text = "";
Alert.show('Record Inserted!', 'Alert Box', mx.controls.Alert.OK);
}
]]>
</mx:Script>
The only code library I import is mx.controls.Alert for my confirmation alert at the end of the script. Then I simply call the InsertEmployee method from the userRequest object, passing the text from my three TextInput controls. Next I blank out the contents of each TextInput and finally pop an alert that the record has been inserted. The result produces this form:
This is very simple, there is no error trapping or validation as we are just demonstrating the act of getting a record from a Flex form into a database using Coldfusion as the server side platform. You can see the results here:
Comments(0)