-
COLDFUSION MX 7 ® Getting Started Building ColdFusion MX Applications
-
Trademarks 1 Step RoboPDF, ActiveEdit, ActiveTest, Authorware, Blue Sky Software, Blue Sky, Breeze, Breezo, Captivate, Central, ColdFusion, Contribute, Database Explorer, Director, Dreamweaver, Fireworks, Flash, FlashCast, FlashHelp, Flash Lite, FlashPaper, Flex, Flex Builder, Fontographer, FreeHand, Generator, HomeSite, JRun, MacRecorder, Macromedia, MXML, RoboEngine, RoboHelp, RoboInfo, RoboPDF, Roundtrip, Roundtrip HTML, Shockwave, SoundEdit, Studio MX, UltraDev, and WebHelp are either registered tradema
-
CONTENTS INTRODUCTION ................................................... 7 PART I: Welcome to ColdFusion CHAPTER 1: Introducing ColdFusion MX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 About ColdFusion MX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 How ColdFusion processes pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Building applications with CFML . . . . . . . . . . . . . . . . . . . . . . .
-
CHAPTER 5: Lesson 2: Configuring Your Development Environment . . . . . . . . . 49 Exercise 1: Verifying the tutorial file structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Exercise 2: Setting up your development environment. . . . . . . . . . . . . . . . . . . . . . 50 Exercise 3: Configuring a database connection . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Exercise 4: Configuring debugging options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Summary. . .
-
CHAPTER 11: Lesson 8: Implementing Browsing and Searching . . . . . . . . . . . . 123 Exercise 1: Enabling users to browse trip details . . . . . . . . . . . . . . . . . . . . . . . . . Exercise 2: Determining actions based on which button a user clicks. . . . . . . . . . Exercise 3: Enabling searching from the Trip Detail page . . . . . . . . . . . . . . . . . . Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
-
Contents
-
INTRODUCTION Getting Started Building ColdFusion MX Applications is intended for anyone who needs to begin programming in the Macromedia ColdFusion MX 7 development environment. This manual includes a tutorial, which uses supporting files that are installed if you chose to install sample applications. If you did not install the ColdFusion MX sample applications, you can download the tutorial files from the ColdFusion Support center on the Macromedia website.
-
Book Description CFML Reference Provides descriptions, syntax, usage, and code examples for all ColdFusion tags, functions, and variables. This manual includes two volumes. To see this manual, go to www.macromedia.com/go/ livedocs_cfmx7docs__cfml_reference. CFML Quick Reference Shows the syntax of ColdFusion tags, functions, and variables in a brief guide. Viewing online documentation All ColdFusion MX documentation is available online in HTML and Adobe Acrobat Portable Document Format (PDF) files.
-
Part I provides an introduction to Macromedia ColdFusion MX 7. It defines ColdFusion MX 7 and provides an overview of the ColdFusion Markup Language (CFML). It also provides generic database concepts. The following chapters are included in Part I: Chapter 1: Introducing ColdFusion MX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Chapter 2: CFML Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
-
-
CHAPTER 1 Introducing ColdFusion MX This chapter introduces the core technologies that are the foundation for Macromedia ColdFusion MX. In addition, it introduces the basic concepts about ColdFusion MX, how it works, and the various components that comprise it. Contents About ColdFusion MX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 How ColdFusion processes pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
-
The ColdFusion Markup Language ColdFusion Markup Language (CFML) is a tag-based language similar to HTML that uses special tags and functions. With CFML, you can enhance your standard HTML files with database commands, conditional operators, and high-level formatting functions, and rapidly produce easyto-maintain web applications. CFML looks similar to HTML: it includes start and end tags, and each tag is enclosed in angle brackets.
-
Building applications with CFML You build ColdFusion applications as a series of pages that use CFML. Developers can extend this language by creating their own custom tags or user-defined functions (UDF), or by integrating COM, C++, and Java components, such as Java Server Page (JSP) tag libraries. Interacting with data sources ColdFusion applications can interact with any database that supports a JDBC technology-based driver.
-
Chapter 1: Introducing ColdFusion MX
-
CHAPTER 2 CFML Basics This chapter introduces the basic elements of CFML, including how to create Macromedia ColdFusion pages, and use variables, functions, conditional processing, and form processing. Contents Working with ColdFusion pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 About CFML elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Working with CFML expressions. . . . . . .
-
The best choice for creating ColdFusion pages is Macromedia Dreamweaver MX. Dreamweaver MX includes many CFML features for building applications, such as rapid visual development, robust CFML editing, and integrated debugging. Dreamweaver MX also includes a copy of HomeSite+ for users who are familiar with developing their application code using ColdFusion Studio or HomeSite 5. HomeSite+ combines all the features of ColdFusion Studio and HomeSite 5, along with support for the latest ColdFusion MX tags.
-
Saving your ColdFusion page In order for the ColdFusion server to process the page, you must save the ColdFusion page on a computer where the ColdFusion MX is installed. If you are creating your pages on a local server (on which ColdFusion is running), you can save the pages locally; if you are using a remote server, you must save your pages on that server. If you are using the ColdFusion MX J2EE configuration, you typically save ColdFusion pages under the ColdFusion web application root.
-
The following figure shows the cfpage.cfm page in the browser: 2. Do the following tasks: a View the source code that was returned to the browser. In most browsers, you can view the source by right-clicking on the page, and then selecting View Source. b Compare the browser source code with the source code that appears in your editor. Notice that the CFML tags were processed on the page but did not appear in the source that was returned to your browser.
-
Most often the end tag encloses the tag name in brackets and includes a forward slash (/), like this: The information processed by ColdFusion is placed between the start and end tag, like this: info to be processed ... ColdFusion tags, for the most part, share these common characteristics: • All start with cf. • Include a start and end tag. • Use attributes (like HTML tags), and most attributes have values. Some ColdFusion tags, such as cfset, omit the ending tag.
-
Functions and number signs You use number signs (#) with functions to display the results of a function on the page.
-
Using functions on values Usually, a function performs an operation on a value, and the value can include the value of a variable. For example, to format the value of a variable that contains a value in dollars, the code to write this statement might look like this: #DollarFormat(price)# The DollarFormat function returns a value as a string and formats that value with two decimal places, a thousands separator, and a dollar sign.
-
In the following examples, the variables are assigned a string literal value. All string literal values are surrounded by double-quotation marks. In the next example, ColdFusion uses the values of the my_first_name and my_last_name variables to set the value for the my_full_name variable in the last line of code.
-
Displaying variable output Output is what remains after the ColdFusion server processes the CFML tags on a page. Usually the output has two parts: • Information that the user sees (for example, a confirmation message) • Information that is stored by the server as a result of processing (for example, user input collected from a form) One of the tags that ColdFusion provides to display output is the cfoutput tag.
-
Building expressions In ColdFusion, you build expressions as you need them. The expressions can include simple elements, such as the expressions shown previously, or they can include complex elements, such as arithmetic functions, strings, and decision operators. (You build some complex expressions in Part II of this manual.) As mentioned, it is important that elements are identified properly in your expression so that ColdFusion processes them as expected, and you avoid unnecessary errors.
-
For more information and examples on using number signs in expressions, see ColdFusion MX Developer’s Guide. Specifying quotation marks around values When you assign literal values to variables, you must surround the literal value with single- or double-quotation marks.
-
The following table lists the unsupported logical operators and their equivalent ColdFusion operators: Unsupported Equivalent ColdFusion logical operator decision operator Description = IS, EQUAL, EQ Tests for equality. < LT, LESS THAN Tests for less than. <= LTE, LE, LESS THAN OR EQUAL TO Tests for less than or equal to. > GT GREATER THAN Tests for greater than. >= GTE, GREATER THAN OR EQUAL TO Tests for greater than or equal to. <> IS NOT, NEQ, NOT EQUAL Tests for nonequality.
-
String operator The following table describes the one ColdFusion string operator that is a concatenation operator: Operator Description & Concatenates strings. About conditional processing So far, all the coding examples shown in this chapter are considered linear coding examples. Linear code is when ColdFusion executes code starting with the first line on the page, and processes every line in order.
-
Using the cfelseif tag to evaluate multiple expressions To evaluate multiple expressions in a cfif statement, you can use cfelseif and cfelse tags in your statement; for example: HTML and CFML tags HTML and CFML tags executed if expression 1 is True. 2> executed if expression 2 is True. executed for expression(s) that is False. The following example shows you how you can evaluate multiple expressions using these tags.
-
When a user submits a form, the form values are stored in form variables and sent to the action page for processing. The following figure shows the relationship between the form page and action page: Form page Name E-mail Submit form.var1=value1 form.var2=value2 The action page contains the code for form processing and interaction with database.
-
Commenting your code As in other programming languages, it is important to include comments in your code. You should comment your code for the following reasons: • Commented code is easier to debug than code that is not commented. • If you describe the code on the page, it is easier to make modifications. • Commented code tends to be better organized.
-
CHAPTER 3 Database Fundamentals This chapter provides a quick overview of relational database concepts and terms. It describes what a database is and how it is organized. It also discusses the Structured Query Language (SQL) that you use to interact with databases. Contents About database basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 About SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
-
What is a relational database? A relational database is a structured collection of information that is related to a particular subject or purpose, such as an inventory database or a human resources database. You use databases to manage information. Information, such as product name, cost, and on-hand inventory, is stored in a database. Within the database, you organize the data into storage containers called tables. Tables are made up of columns and rows. Columns represent individual fields in a table.
-
About relational tables In a database, you can organize data in multiple tables.
-
About SQL SQL (Structured Query Language) is a language that lets you communicate with databases. For example, you can use SQL to retrieve data from a database, add data to a database, delete or update records in a database, change columns in multiple rows, add columns to tables, and add and delete tables. Unlike other computer languages, SQL is made up of a small number of language elements that let you interact efficiently with a database.
-
Using the SQL WHERE clause to limit the rows returned To conditionally select data from a table, you can add a WHERE clause to the SELECT statement, which results in the following syntax: SELECT column_name FROM table_name WHERE column condition value With the WHERE clause, you can use any of the following operators: Operator Description = Equal <> Not equal > Greater than < Less than >= Greater than or equal <= Less than or equal BETWEEN Between an inclusive range AND Joins one or more co
-
The result of the preceding SQL statement contains the following data: LastName FirstName Brown Marie Note: The preceding SQL SELECT examples use single-quotation marks around the value. SQL uses single-quotation marks around text values. Most database systems also accept doublequotation marks. Do not enclose numeric values in quotation marks. Sorting the results You use the ORDER BY clause to sort the result rows.
-
Writing SQL and CFML statements to interact with a data source After ColdFusion makes a connection to the data source, you can interact with that database by using SQL and ColdFusion. To interact with an established data source, you must include SQL statements in your CFML statements; for example: SELECT FirstName, LastName, DepartmentID From Employee In this example, the first attribute of the cfquery tag is the name of the query.
-
Chapter 3: Database Fundamentals
-
Part II provides a tutorial that steps you through building a sample Macromedia ColdFusion application. It consists of the following lessons: Lesson 1: Preparing to Build the Sample Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Lesson 2: Configuring Your Development Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Lesson 3: Retrieving Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
-
-
CHAPTER 4 Lesson 1: Preparing to Build the Sample Application This tutorial guides you through the process of building a simple Macromedia ColdFusion web application. By the end of the tutorial, you will be familiar with how to use Macromedia ColdFusion MX 7 to query and update a database, validate data, generate a report, and require user authentication. The application that you will build in this tutorial is for a fictitious travel company called Compass Travel.
-
Step Description 3 Design the database for your application by exploring the database tables that will store the trip information. 4 Develop the ColdFusion application pages. The following sections explore an overview of each of these application development steps. Exercise 1: Determining the application functional requirements Before you can build the sample application, you must understand the functional requirements that influence its design.
-
Exercise 2: Determining the data requirements Prior to creating the application pages to capture trip information, you must determine what type of data is required about each trip.
-
If you had to create the Compass Travel database, you would create a table named trips to store the information that you plan to collect about each trip. The table would look something like the following table: Recognizing the data types Each field in the Trips table has a data type attribute that describes the type of data that can be stored in the column. For instance, the tripName column can contain text data, while the price column can only contain numeric data.
-
Establishing a relationship between the two tables When the user selects an event type from the list obtained from reading the eventtypes table, the correct event type must be saved to the trips table with all the other trip-related data. The application could store the eventType (for example, mountain climbing) itself into the eventType column in the trips table.
-
Exercise 4: Developing the sample application Using the application functional requirements and the database that is provided for this tutorial, you are ready to use ColdFusion to develop the Trip Maintenance application. The remaining lessons in the tutorial step you through the process of constructing this application.
-
• Implement browsing and searching (“Lesson 8: Implementing Browsing and Searching” on page 123). • Enable database maintenance (“Lesson 9: Enabling Database Maintenance” on page 129). • Restrict access to the Compass Travel application by requiring user authentication (“Lesson 10: Restricting Access to ColdFusion Applications” on page 143). How to proceed Each lesson in the tutorial is designed to let you proceed at your own pace.
-
You can view ColdFusion application pages on your local computer by opening a web browser and entering one of the following URLs: Configuration URL For local third-party web http://localhost/cfdocs/getting_started/my_app/.cfm server configuration For built-in ColdFusion http://localhost:8500/cfdocs/getting_started/my_app/.
-
CHAPTER 5 Lesson 2: Configuring Your Development Environment In this lesson, you set up your development environment for the tutorial. This lesson describes the tutorial file structure, and how to configure the database connection and debugging options. Additionally, it provides an overview of using Macromedia Dreamweaver MX for Macromedia ColdFusion development. This lesson explains how to do the following tasks: Exercise 1: Verifying the tutorial file structure . . . . . . . . . . . . . . . . . . . . . .
-
Note: When you use the J2EE configuration, the cfdocs directory is under the ColdFusion web application root. For more information, see “Saving your ColdFusion page” on page 17. ColdFusion MX installs two copies of the sample CompassTravel database file. The working copy is located in the db directory; a backup copy of the file is in the new_user_database directory.
-
With Dreamweaver MX, you can author and test your application code from a local or remote client. You can save your code directly to the server computer where ColdFusion is installed. The following sections provide an overview of Dreamweaver MX, and information on how to configure Dreamweaver MX for ColdFusion development.
-
Exercise 3: Configuring a database connection One of the most commonly used and most powerful features of ColdFusion is the ability to connect to and manipulate data from a wide variety of databases. Like most ColdFusion developers, you will probably use this capability often. To be able to connect to a database, you must create a a data source in ColdFusion. A data source contains the information that ColdFusion needs to be able to recognize and communicate with a database.
-
To access the ColdFusion MX 7 Administrator: • Open a browser and go to one of the following URLs: ■ External web server users: http://localhost/CFIDE/administrator ■ Built-in web server users: http://localhost:8500/CFIDE/administrator ■ J2EE server users: http://localhost:portnumber/CFIDE/administrator. If you installed ColdFusion Enterprise Edition with JRun, the default HTTP port is 8300.
-
To define the sample PointBase database file: 1. In the ColdFusion MX Administrator, select Data & Services > Data Sources. The Add New Data Source dialog box appears. 2. Specify the following: Field Action Data Source name text box Specify the name CompassTravel. Note: Ensure that the name of the data source file does not contain any spaces. If the name contains a space, the data source connection fails. Driver drop-down selection box Select Other. 3.
-
7. Click Verify All Connections to ensure that ColdFusion can access this file. OK appears in the Status column for successful connections. If the connection to the compass travel data source fails, do the following: a Verify that the name of the data source file does not contain a space. If it does contain a space, delete the data source from the Connected Data Source dialog box.
-
The location of the debugging information or the type of debugging data shown varies, depending on the options that you enable on the Debugging page in the ColdFusion MX Administrator.
-
Sending debugging information to remote clients If you are using a remote client to perform the tutorial in this book, you must specify your IP address to receive debugging information. If you are working on a local client (the computer where ColdFusion is installed), you do not need to do this. To receive debugging information when using a remote client: 1. In the ColdFusion MX Administrator, select Debugging & Logging > Debugging IP Addresses. The Debugging IP Address page appears. 2.
-
Chapter 5: Lesson 2: Configuring Your Development Environment
-
CHAPTER 6 Lesson 3: Retrieving Data In this lesson, you begin the construction of a Macromedia ColdFusion web application for the fictitious company, Compass Travel. The exercises in this lesson introduce you to the process of retrieving data from the Compass Travel relational database. This lesson contains the following exercises: Exercise 1: About the trip list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
-
Exercise 1: About the trip list As you recall from “Lesson 1: Preparing to Build the Sample Application” on page 41, one of the requirements for the Trip Maintenance application is the ability to generate trip listings. To help Compass Travel agents take trip reservations by telephone and in person, the trip coordinator maintains a list of current trip offerings. Years ago, the coordinator would type the list and fax it to the various Compass Travel offices in an effort to keep everyone informed.
-
Exercise 2: Retrieving the information for the trip list Relational database management systems process SQL instructions sent to them from various applications. ColdFusion sends SQL statements to database managers to manipulate data. ColdFusion needs a way to know to which database manager to send a specific SQL string for evaluation. In CFML, the cfquery tag serves this purpose.
-
Reviewing the code The following table describes the code used to build the query: Code Explanation ColdFusion query named TripList. Submits any SQL statement between the cfquery start and end tags to the data source specified in the datasource attribute. SELECT trips.tripName FROM trips SQL SELECT statement to retrieve all tripName(s) from the trips table.
-
Trip List
#tripName#