# Structure Example

Structures are particularly useful for grouping together a set of variables under a single name. In the following example files, structures are used to collect information from a form, `structinsert.cfm`, and to submit that information to a custom tag at `addemployee.cfm`.

These example files show how you can use a structure to pass information to a custom tag, named CF_ADDEMPLOYEE.

### Example file structinsert.cfm

    <!--- This example shows how to use the StructInsert
          function. It calls the CF_ADDEMPLOYEE custom tag,
          which uses the addemployee.cfm file. --->
    <HTML>
    <HEAD>
    <TITLE>Add New Employees</TITLE>
    </HEAD>

    <BODY>
    <H1>Add New Employees</H1>

    <!--- Establish parameters for first time through --->

    <CFPARAM NAME="FORM.firstname" DEFAULT="">
    <CFPARAM NAME="FORM.lastname" DEFAULT="">
    <CFPARAM NAME="FORM.email" DEFAULT="">
    <CFPARAM NAME="FORM.phone" DEFAULT="">
    <CFPARAM NAME="FORM.department" DEFAULT="">

    <!--- If all form fields are passed, create structure
        named employee and add values --->

    <CFIF #FORM.FIRSTNAME# EQ "">
     <P>Please fill out the form.
    <CFELSE>
      <CFOUTPUT>
       <CFSCRIPT>
         employee=StructNew();
         StructInsert(employee, "firstname", "#FORM.firstname#");
         StructInsert(employee, "lastname", "#FORM.lastname#");
         StructInsert(employee, "email", "#FORM.email#");
         StructInsert(employee, "phone", "#FORM.phone#");
         StructInsert(employee, "department", "#FORM.department#");
      </CFSCRIPT>

      <P>First name is #StructFind(employee, "firstname")#</P>
      <P>Last name is #StructFind(employee, "lastname")#</P>
      <P>EMail is #StructFind(employee, "email")#</P>
      <P>Phone is #StructFind(employee, "phone")#</P>
      <P>Department is #StructFind(employee, "department")#</P>
      </CFOUTPUT>

      <!--- Call the custom tag that adds employees --->

      <CF_ADDEMPLOYEE EMPINFO="#employee#">
    </CFIF>

    <HR>
    <FORM ACTION="structinsert.cfm" METHOD="Post">
    <P>First Name:&nbsp;
    <INPUT NAME="firstname" TYPE="text" HSPACE="30" MAXLENGTH="30">
    <P>Last Name:&nbsp;
    <INPUT NAME="lastname" TYPE="text" HSPACE="30" MAXLENGTH="30">
    <P>EMail:&nbsp;
    <INPUT NAME="email" TYPE="text" HSPACE="30" MAXLENGTH="30">
    <P>Phone:&nbsp;
    <INPUT NAME="phone" TYPE="text" HSPACE="20" MAXLENGTH="20">
    <P>Department:&nbsp;
    <INPUT NAME="department" TYPE="text" HSPACE="30" MAXLENGTH="30">

    <P>
    <INPUT TYPE="Submit" VALUE="OK">
    </FORM>

    </BODY>
    </HTML>

### Example file addemployee.cfm

    <P>This file is an example of a custom tag used
    to add employees. Employee information is passed
    through the employee structure (the EMPINFO attribute).
    In UNIX, you must also add the Emp_ID.

    <CFSWITCH EXPRESSION="#ThisTag.ExecutionMode#">
        <CFCASE VALUE="start">
            <CFIF StructIsEmpty(attributes.EMPINFO)>
            <CFOUTPUT>Error. No employee data was passed.</CFOUTPUT>
                <CFEXIT METHOD="ExitTag">
            <CFELSE>
            <!--- Add the employee --->
            <!--- In UNIX, you must also add the Emp_ID --->

            <CFQUERY NAME="AddEmployee" DATASOURCE="cfsnippets">
                INSERT INTO Employees
                (FirstName, LastName, Email, Phone, Department)
                VALUES
                <CFOUTPUT>
                (
                '#StructFind(attributes.EMPINFO, "firstname")#' ,
                '#StructFind(attributes.EMPINFO, "lastname")#' ,
                '#StructFind(attributes.EMPINFO, "email")#' ,
                '#StructFind(attributes.EMPINFO, "phone")#' ,
                '#StructFind(attributes.EMPINFO, "department")#'
                 )
                </CFOUTPUT>
            </CFQUERY>
            </CFIF>
        <CFOUTPUT><HR>Employee Add Complete</CFOUTPUT>
        </CFCASE>
    </CFSWITCH>