User Guide

Table Of Contents
120 Chapter 5: Using Arrays and Structures
Example file addemployee.cfm
The following file is an example of a custom tag used to add employees. Employee information is
passed through the employee structure (the empinfo attribute). For databases that do not support
automatic key generation, you must also add the Emp_ID.
<cfif StructIsEmpty(attributes.empinfo)>
<cfoutput>
Error. No employee data was passed.<br>
</cfoutput>
<cfexit method="ExitTag">
<cfelse>
<!--- Add the employee --->
<cfquery name="AddEmployee" datasource="cfdocexamples">
INSERT INTO Employees
(FirstName, LastName, Email, Phone, Department)
VALUES (
'#attributes.empinfo.firstname#' ,
'#attributes.empinfo.lastname#' ,
'#attributes.empinfo.email#' ,
'#attributes.empinfo.phone#' ,
'#attributes.empinfo.department#' )
</cfquery>
</cfif>
<cf_addemployee
empinfo="#duplicate(employee)#">
</cfif>
Call the cf_addemployee custom tag and pass it
a copy of the employee structure in the
empinfo attribute.
The duplicate function ensures that the custom
tag gets a copy of the employee structure, not
the original. While this is not necessary in this
example, it is good practice because it
prevents the custom tag from modifying the
calling page’s structure contents.
<form action="newemployee.cfm" method="Post">
First Name:&nbsp;
<input name="firstname" type="text"
hspace="30"
maxlength="30"><br>
Last Name:&nbsp;
<input name="lastname" type="text" hspace="30"
maxlength="30"><br>
EMail:&nbsp;
<input name="email" type="text" hspace="30"
maxlength="30"><br>
Phone:&nbsp;
<input name="phone" type="text" hspace="20"
maxlength="20"><br>
<p>Department:&nbsp;
<input name="department" type="text"
hspace="30"
maxlength="30"><br>
<br>
<input type="Submit" value="OK">
</form>
The data form. When the user clicks Submit,
the form posts the data to this ColdFusion
page.
Code Description