Adobe COLDFUSION 9 Manual page 109

Developing applications
Hide thumbs Also See for COLDFUSION 9:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
The CFML Programming Language
Code
<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="">
<cfif #Form.firstname# eq "">
<p>Please fill out the form.</p>
<cfelse>
<cfoutput>
<cfscript>
employee=StructNew();
employee.firstname = Form.firstname;
employee.lastname = Form.lastname;
employee.email = Form.email;
employee.phone = Form.phone;
employee.department = Form.department;
</cfscript>
<!--- Display results of creating the structure. --->
First name is #StructFind(employee, "firstname")#<br>
Last name is #StructFind(employee, "lastname")#<br>
EMail is #StructFind(employee, "email")#<br>
Phone is #StructFind(employee, "phone")#<br>
Department is #StructFind(employee, "department")#<br>
</cfoutput>
<cf_addemployee empinfo="#employee#">
</cfif>
<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>
Department:&nbsp;
<input name="department" type="text" hspace="30"
maxlength="30"><br>
<input type="Submit" value="OK">
</form>
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
Emp_ID.
attribute). For databases that do not support automatic key generation, also add the
Last updated 8/5/2010
Description
Set default values of all form fields so that they exist the first
time this page is displayed and can be tested.
Test the value of the firstname field. This field is required. The
test is False the first time the page displays.
If no data exists in the Form.firstname variable, display a
message requesting the user to fill the form.
If Form.firstname contains text, the user submitted the form.
Use CFScript to create a structure named employee and fill it
with the form field data.
Then display the contents of the structure.
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. Although this
is not necessary in this example, it is good practice because it
prevents the custom tag from modifying the calling contents of
the structure in the calling page.
The data form. When the user clicks OK, the form posts the data
to this ColdFusion page.
104

Advertisement

Table of Contents
loading

Table of Contents