Creating Simple Dynamic Pages  
 
 

Creating dynamic Web pages with ColdFusion is as straightforward as creating ordinary Web pages. The following sections will step you through two simple examples designed to introduce you to ColdFusion. If you're feeling more adventurous, take a look at the sections that deal with the example applications installed with ColdFusion in

 
 
  Example 1: A simple dynamic page  
 
 

This example shows how ColdFusion uses a form field variable entered into an HTML form.

Using ColdFusion Studio, create two files in the \cfdocs directory located in your Web server document root directory:

  • hellopage1.cfm
  • hellopage2.cfm
  1. In the hellopage1.cfm file, type the following HTML and CFML, or copy and paste into a new document if you are viewing this online:
    <HTML>
    <HEAD>
    <TITLE>Hello World Page One</TITLE>
    </HEAD>
    <BODY>
    
    <H2>Enter Your Message</H2>
    <HR>
    
    <FORM ACTION="hellopage2.cfm" METHOD="post">
    <INPUT TYPE="text" SIZE="30" NAME="HelloText">
    <INPUT TYPE="submit" VALUE="Submit">
    </FORM>
    
    </BODY>
    </HTML> 
    
  2. In the hellopage2.cfm file type the following HTML and CFML and save:
    <HTML>
    <HEAD>
    <TITLE>Hello World Page Two</TITLE>
    </HEAD>
    <BODY>
    
    <CFOUTPUT>
    <H2>#Form.HelloText#</H2>
    </CFOUTPUT>
    
    </BODY>
    </HTML> 
    
  3. Make sure that ColdFusion and your Web server are running.
  4. Open a Web browser and enter the following URL to open your new ColdFusion page:
    http://127.0.0.1/cfdocs/hellopage1.cfm
    

Enter your message

  1. In your browser, enter text into the form and click Submit. The page that appears displays the text you just entered.

Where is Harrison?

 
 
  Explanation  
 

When the form was submitted in the first page, a variable was created (Form.HelloText) and passed to the second page where it was resolved and displayed.

 
 
  Example 2: A database-driven page  
 
 

The following example shows you how to create an application page using data from the ColdFusion cfsnippets data source.

  1. Create a file called courses.cfm in the \cfdocs\ directory located in your Web server root document directory.
  2. Type the following HTML and CFML in the courses.cfm file and save:
    <!--- Start of Database Query ---> 
    <CFQUERY NAME= "CourseList" DATASOURCE= "cfsnippets">
    SELECT * FROM CourseList
    </CFQUERY>
    <!--- End of Query --->
    
    <HTML>
    <HEAD>
    <TITLE>Department List</TITLE>
    </HEAD>
    
    <BODY>
    
    <H2>Course List</H2>
    <HR>
    
    <!--- Start of Output Block --->
    <CFOUTPUT QUERY="CourseList">
    (#CourseNumber#)</B> #CourseName#<BR>
    </CFOUTPUT>
    <!--- End of Output Block --->
    </BODY>
    </HTML> 
    

    Note the use of the comment convention used in ColdFusion pages: <!--- and --->. ColdFusion comments use a form similar to HTML comments, but with an additional hyphen.

  3. Make sure that ColdFusion and your Web server are running.
  4. Open a Web browser and enter the following URL to open your new application page:
    http://127.0.0.1/cfdocs/courses.cfm
    
  5. The page that appears shows a list of course numbers and names.

Course List

 
 
  Explanation  
 

In this example, the CFQUERY tag used the SQL SELECT statement to retrieve all the data from the CourseList table. The CFOUPUT tag then referenced the CourseNumber and CourseName columns returned by the query, for display.

Once again, here's the query part:

<!--- Start of Database Query ---> 
<CFQUERY NAME= "CourseList" DATASOURCE= "cfsnippets">
SELECT * FROM CourseList
</CFQUERY>
<!--- End of Query --->

Here's the output part:

<CFOUTPUT QUERY="CourseList">
(#CourseNumber#)</B> #CourseName#<BR>
</CFOUTPUT>


 
 
BackUp LevelNext
 
 

allaire     AllaireDoc@allaire.com
    Copyright © 1998, Allaire Corporation. All rights reserved.