Accessibility

Table of Contents

Creating a Client-Server Guest Book with Macromedia Flash and ColdFusion

Writing the ColdFusion Component

You will use a ColdFusion component to get values from the database and send them to Flash (see Figure 2).

Interaction between Flash, ColdFusion, and your database

Figure 2. Interaction between Flash, ColdFusion, and your database

To write the CFC, follow these steps:

  1. Open Dreamweaver.
  2. Create a new CFC by choosing File > New > General > Dynamic Page > ColdFusion Component (see Figure 3) and then clicking Create.

    Creating a CFC in Dreamweaver

    Figure 3. Creating a CFC in Dreamweaver

  3. Delete the code that Dreamweaver inserts.
  4. Copy and paste the following code in the CFC. It defines a method to insert a new record into the database:

    <cfcomponent hint="Insert and Get Users Comments">
    	<!--- Define a function to add comments --->
    	<cffunction name="InsertNewComment" access="remote" returntype="string" hint="Add new comment">
    		<!--- Define required parameters --->
    		<cfargument name="FirstName" type="string" required="yes">
    		<cfargument name="LastName"  type="string" required="yes">
    		<cfargument name="Email"     type="string" required="yes">
    		<cfargument name="Message"   type="string" required="yes">
    		<!--- Get Current Date --->
    		<cfset Date="#DateFormat(Now(), 'yyyy/mm/dd')#">
    		<!--- Insert new record into the DataBase --->
    		<cfquery datasource="GuestBook">
    		INSERT INTO tblContent
    		VALUES('#FirstName#',
    		       '#LastName#',
    		       '#Email#',
    		       '#Date#',
    		       '#Message#')
    	                </cfquery>
    	<!--- Specify result as string --->
    	<cfset result="Your comment has been added successfully.">
    	<!--- Return result of the function --->
    	<cfreturn result>
    	</cffunction>
    </cfcomponent>
    

    This method, InsertNewComment, requires five parameters to insert a new record into the database: FirstName, LastName, Email, Message, and Date. The user enters the first four parameters; ColdFusion generates Date at runtime automatically.

  5. To obtain a list of all records, append the following code below the previous function (after the closing </cffunction> tag but before the closing </cfcomponent> tag):

    <!--- Obtain Comments --->
    	<cffunction name="GetComments" access="remote" returntype="query" hint="Get a list of comments">
    		<!--- Get all fields from DataBase --->
    		<cfquery datasource="GuestBook" name="Info">
    		SELECT *
    		FROM tblContent
    		ORDER BY "Date" DESC
    		</cfquery>
    	<!--- Return result of the function --->
    	<cfreturn Info>		
    	</cffunction>
    
  6. If you haven't already created a new folder in the wwwroot directory named cfGuestBook, create it now. Save this CFC as GuestBook.cfc within the cfGuestBook folder.

Now that you have built the ColdFusion component, you can create its Flash interface.