In: Categories » Computers and technology » Programming » First ColdFusion Application
You learn by doing, and then we go back and explain how ColdFusion worked its magic. Before you get started, though, you must learn a few terms, set up the database, and create the Web directory that you’re going to use. These are some terms that you should know:
A template is a file with a .cfm extension. ColdFusion executes these templates and produces HTML that is returned to the user’s browser.
A page is what appears in your browser. It is rendered from the HTML that ColdFusion Server sends back to your browser.
Your browser requests a ColdFusion template. ColdFusion processes the template into HTML and sends it to your Web server, and your Web server responds by sending this HTML to your browser, which renders the HTML into the Web page that you see. You need three things to build your application:
Macromedia ColdFusion MX Server, installed and running.
Macromedia HomeSite or HomeSite+ (or the similar but older ColdFusion Studio editor). You use HomeSite to develop your example application.
A Web browser.
Create a directory in your Web root (usually c:\inetpub\wwwroot\) named CFMXBible. Inside this directory, create a subdirectory named Ch02. All the templates that you create in this article go into this directory. We have provided a sample Access database named CFMXBible.mdb. Follow these steps to set up the database:
1. Copy CFMXBible.mdb to the root of your server’s C: drive. Notice that you don’t place this file inside of the Web root, because if you make the MDB file Web-accessible, anyone can download your MDB file right from their browser. Placing this file outside the Web root offers an extra measure of security.
2. Log in to ColdFusion Administrator, which is the control console for ColdFusion MX. You can go to ColdFusion Administrator through a shortcut in the Macromedia ColdFusion MX group on your server’s Start menu, or you can go to ColdFusion Administrator directly by pointing your Web browser to http://<yourserver>/cfide/administrator (replacing <yourserver> with the name of the server on which you develop your example application).
3. Click the Data Sources link on the left side of your browser.
4. In the Add New Data Source part that appears, enter CFMXBibleAccess in the Data Source Name field; choose Microsoft Access as the driver type; and Click Add.
5. The CF Data Source Name text box should already contain CFMXBibleAccess. Enter the path to your MDB file (which should be C:\CFMXBible.mdb) in the Database File field, and leave System Database File blank. Leave Use Default Username checked; enter a meaningful description in the Description field; and click Submit.
If you see the message datasource updated successfully, you are ready to build your example application. If you don’t see this message, the datasource was set up incorrectly. Work through these steps again to resolve your problems before you go any further. An application is a collection of processes. The application that you’re about to build, for example, among other things, adds new companies and employees to the database. Adding a company is one process in your application, and adding an employee is a second process. The user must also get a list of the companies and employees in the database, update information about a specific company or employee, and delete employees and companies from the database—more processes to consider in designing your application. Real-world process design is quite involved, often employing dedicated process-design tools, but at the very least, you should sketch a flowchart containing all processes that your application should support before you start creating directories, databases, and code. Before you write an application, you should know the structure and nature of the data that your processes use. Will the application manage a store’s inventory, blocks of content on a news site, or something completely different? Your application in this article, for example, manages companies and employees. Each company has a name and address, comments about the company, and an identifier, or key, that the database uses to keep track of which company is which. Employees have a social security number (SSN), which identifies each employee (the employee’s key), as well as a name, salary, date of birth, and the identifier, or key, of the company for which each employee works. Now that you’ve decided what your application will do, you must create the basic framework within which your application is to run. To do so, follow these steps:
1. Launch HomeSite or a similar code editor of your choice.
2. Choose your C drive from the drop-down list at the top left of the HomeSite window.
3. Scroll down to the Inetpub directory in the tree below the drop-down list.
4. Expand Inetpub if using IIS (Internet Information Server), or CFusionMX if using the standalone web server.
5. Expand wwwroot.
6. Expand CFMXBible.
7. Select the Ch02 directory that you created earlier.
8. Choose File➪New Document from the menu.
9. Select everything in the main editing window in HomeSite and press the Delete key to remove any default code.
10. Choose File➪Save from the menu and make sure that you are in the Ch02 directory (it should appear in the drop-down list above the list of files).
11. Name your new file Application.cfm and click Save. Application.cfm is a special template that ColdFusion automatically executes before each template that you specifically request. Make sure that the initial A is capitalized in the file name or it will not work on some platforms.
12. Type the code into the editing window and then save the file as Application.cfm.
<!--- Give your application a name ---> <cfapplication name=”CH02”> <!--- This is the data source your application will use ---> <cfset Request.MainDSN = “CFMXBibleAccess”>
The CFAPPLICATION tag tells ColdFusion Server that all the templates and variables in your application are part of an application space named Ch02 within which all the application’s variables and data will be contained, and Request.MainDSN contains the name of the datasource that you created earlier. Now that you’ve set up your application directory and database and created a basic framework, you write code to add a company to the database. You create three templates: a form template, an action template, and a template that displays a message when the action has finished. The form template collects data about the company; the action template puts it into the database and redirects the user to the finished template; and the finished template tells the user that the action was successful. The first template that you create is the form that collects company data. This form will contain input fields for the company name, address, city, state, and zip code, plus a submit button. Create a file named CompanyAddForm.cfm inside the Ch02 directory, type the code into the file’s editing window, and save the file.
<html> <head> <title>ColdFusion MX Bible</title> <link rel=”stylesheet” href=”styles.css”> </head> <body> <h1>Add a Company</h1> <table> <cfform action=”CompanyAddAction.cfm” method=”POST”> <tr>
<td>Company Name</td> <td> <cfinput type=”Text” name=”CompanyName” message=”Please enter a name for this company.” required=”Yes” size=”40” maxlength=”40”> </td> </tr> <tr> <td>Address</td> <td> <cfinput type=”Text” name=”Address” message=”Please enter this new Company’s Address.” required=”Yes” size=”32” maxlength=”30”> </td> </tr> <tr> <td>City</td> <td> <cfinput type=”Text” name=”City” message=”Please enter a city.” required=”Yes” size=”22” maxlength=”20”> </td> </tr> <tr> <td>State</td> <td> <cfinput type=”Text” name=”State” message=”Please enter a state.” required=”Yes” size=”3” maxlength=”2”> </td> </tr> <tr> <td>ZIP Code</td> <td> <cfinput type=”Text” name=”ZipCode” message=”Please enter a valid ZIP Code.” validate=”zipcode” required=”Yes”
size=”11” maxlength=”10”> </td> </tr> <tr> <td>Comments</td> <td> <textarea cols=”40” rows=”5” name=”Comments”></textarea> </td> </tr> <tr> <td> </td> <td> <input type=”submit” value=”Add to Database”> </td> </tr> </cfform> </table> </body> </html>
This code is a lot to digest, so we break it down as follows:
The TABLE, TR and TD tags define the visual layout of this form. This is plain HTML.
CFFORM is the container for the data that the user enters into the form controls, which is passed to the server after the user submits the form.
The submit button (INPUT type=”submit”) submits the form data to the action page, which will take that data and insert it into the database. You build the action page next. The CFFORM in CompanyAddForm.cfm has two attributes: Method and Action. The form’s Method is POST, and its Action is CompanyAddAction.cfm, meaning that when the user clicks Submit, the form data is posted to the CompanyAddAction.cfm template. Each of the CFINPUT tags has a Type attribute. In this application, the Type is always TEXT, which means that the input field is a single-line text field. Each of the CFINPUT tags has a Name attribute, which is the name of the Form variable that contains the data entered into the field after the form is submitted to the action template.
legal notice
Our website is not responsible for the information contained by this article. Web-articles is a free articles resource.
Suggestion: If you need fresh, daily updated content for your website, feel free to use our service. Click here for more information.
Useful tools and features
related articles
The DateFormat() function around the DateOfBirth column in the codereturns the employee’s birth date reformatted according to a display mask. DateOfBirth normally comes back from the database in the following format: 2002-01-01 00:00:00.0 That format is not very user-friendly. Calling DateFormat() with a mask of “mm/dd/yyyy” returns the date as follows: 01/01/2002 This version is, of course, more natural and easy to read. The same is true for the employee edit process...
2. Removing an Employee From the Database
The user must have the capability to remove employees from the database. The employee delete process is a simple combination of techniques that you have already learned, such as retrieving a record from the database, displaying that record in a template, and so on. The first page in this process is nearly identical to the employee get edit form. Create a file named EmployeeGetDeleteForm.cfm inside the Ch02 directory, type the code into the file’s editing window, and save the file. <html> <head> <ti...
3. Making direct links to the forms
Say that you want to modify or delete a company. Right now, you need to remember the company’s ID, go back to the launch pad, click Company Edit, and enter the company ID, all just to get to the edit form. Wouldn’t you rather click a company in the list and go directly to the edit form? ... <table> <tr> <td><b>ID</b></td> <td><b>Name</b></td> <td><b>Address</b></td> <td><b>City</b></t...
4. Using a Custom Tag
Custom tags are reusable, developer-authored extensions to the ColdFusion language. The custom tag that you create in the following parts displays today’s date in a familiar format. Create a file named TodaysDate.cfm inside the Ch02 directory, type the code into the file’s editing window, and save the file. <cfoutput>#DateFormat(Now(), “ddd, mmm d, yyyy”)#</cfoutput> Now() returns the current date and time, and DateFormat() reformats the date. To call your custom tag, open index.cfm, a...
5. Creating your physical data model
Physical data models are extracted from their logical counterparts by mapping logical objects to physical objects supported by the target database platform. The following table shows the relationship between logical and physical objects. In many cases, your physical data model almost mirrors your logical data model because entities typically map directly to the tables that store them. Exceptions to this rule are logical data models that contain nonspecific relationships and entity subtypes. Although entity subtypes are an advanced top...
6. Understanding All Relational Result Sets
This article can help you better understand complicated SQL containing multitable joins, group-related clauses, and aggregate functions—by far the most problematic topics for most database developers. You also learn the correct way to handle database exceptions and incorporate them as actual functionality in your ColdFusion application. You learn, too, how to increase performance by caching queries in memory for fast access. You can memorize SQL clauses and Bachus-Naur forms until you’re blue in the face, ...
7. How and when to use Group by Having and Distinct
After learning how SQL works internally, you can better learn how to put the more complicated aspects of it to use. First we need to clarify the difference between GROUP BY and DISTINCT. Quite a bit of confusion exists over these two clauses, because they often produce the same query results but for very different reasons. In fact, GROUP BY and DISTINCT are not related in any way whatsoever. The GROUP BY clause produces as its intermediate work product a table of rows representing each group, and a sort of “invisible third dimen...
