Coldfusion fundamentals

what is coldfusion?

  • Cold Fusion is one of many middleware" technologies that allow you
    • to process information,
    • control databases and interface with legacy systems
    • using a TAG-Based Syntax, similar to HTML/XML
  • Cold Fusion Markup Language (CFML) executes on the server, processes the requests based on conditions and returns HTML to the requesting client (requesting) browser.
  • There are 2 components to Cold Fusion

    Cold Fusion Application Server - Server Software (Parsing Engine)
    Cold Fusion Markup Language (CFML) - Scripting language

how it works

Client Submits request from a Web browser to a CFML Script On a web server. Webserver sends the CFML script and any client Data to the Cold Fusion Server for processing Cold Fusion Server Processes File, collects Data from datasources, And produces HTML OUTPUT

Client Receives the HTML Formatted Document within the Web Browser.   Cold fusion Server sends the HTML output to the Webserver for delivery to the Requesting Browser

 

One difference is that for you cfml code to work, your web document will need to have the extension .cfm instead of .htm. This let's the server software know that it needs to process the incoming file as a cfml file and do some processing.

All CFML tags start with a CF which allows to differentiate them from regular HTML tags.

CFML Tags can be used to perform a variety of tasks. They are divided into the followijhng categories

  • Application framework
  • Database manipulation
  • Data output
  • External Interfaces
  • File manipulation
  • Flow control
  • Forms
  • Internet protocols
  • Security
  • Utilities
  • Searching

 

BASIC CFML tags

BASIC CFML Tags that we will learn:

TAGs USE

<CFSET> - Set a Variable

<CFSET> is used to assign a value to a variable. it is also used to read input from an HTML form and from data passed through a hyperlink.

The variable is a given name that allows to recall a specific value whenever needed

The cfset tag has NO ATTRIBUTES

<CFSET FirstName= "Barry">

In this first example, a variable called firstname is being set to the string barry

<cfset x = 47>

Here a variable called x is begin set to the value 47
<cfset y = 3>
<cfset sum = x + y>

Notice the use of the "". When we are using a string with use the "" otherwise, no as we want the value of the number to be read.

 

<CFOUTPUT></CFOUTPUT> - Variable Output

This tag has two main use:

  1. output database query information
  2. output the value of coldfusion variables and functions

<CFOUTPUT> #firstname#</CFOUTPUT> - Variable Output

CFML also uses Fonctions, which like we saw we javascript have the syntax: Functionname(Argument)
now() is a cfml function returns the current date and time

<cfset date =now()>

<cfoutput> today is #date#</cfoutput>

 

 

Dateformat(Date[, mask])

allows you to set the way in which we will see the date

<CFSET today = #dateformat(now(), "MMMM DD, YYYY")#>

this will show today's date as: full name of the month (MMMM) the day number and the year. i.e. March 17, 2003

 

Coldfusion comments: <!--- ---> are similar to html comments, but instead of <!-- --> we use three dashes

 
 

<CFINCLUDE>: is used to include code from one template into another.

Common code reuse include web page4 headers, footers, any information that get repeated often in the site.

<cfinclude template="date.cfm">

 

So, create the first template p. 31 of the book

--- You should have two files, MyFirstTemplate.cfm and a Copyrights.cfm file

<!---
File:
Description:
Author:
Created:
--->

<HTML>
< HEAD>
<TITLE>My First Template</TITLE>
< /HEAD>
< !--- set the value of the FirstName variable to your name --->
< CFSET FirstName= "Barry">

<BODY>
<CFOUTPUT>
<H1>Welcome #FirstName#</H1>
Today's date is: #DateFormat(Now(), "DD MM, YYYY")#
</CFOUTPUT>
<HR>
<!--- include copyright information --->
<CFINCLUDE TEMPLATE="Copyright.htm">

< /BODY>
< /HTML>

 

 

CFML TAGS: Variables

The Cold Fusion Variable is very similar to the FLASH OR jAVASCRIPT Variable.

  • A Variable allows you to store and process information retrieved from a source: (either: User Input, Database Query, Data Syndication, or Time Condition)
  • Variables can be manipulated using FUNCTIONS built into the Application server.
  • Variables have SCOPES to help define them.

Variable SCOPES:

  • Local: Variables created using cfset or cfparam, with or without specifying the scope prefix. You must define the variable on the current page or a page you include using cfinclude.
  • URL: Variables passed to a page as URL query string parameters.
  • FORM: Form Form Data entered in tags in an HTML form or ColdFusion cfform tag block and processed on an action page.
  • Query:
  • Client / Session / Application / CGI

Variables are also set using Submitted HTML FORMs

When a user CLICKS submit. The “ACTION” Page receives the contents of the form as either FORM-Scoped Variables or URL-Scoped Variables.

<FORM Method=“GET”> -- will send URL Variables to the Action Page
<FORM Method=“POST”> -- will send FORM Variables to the Action Page

There are two ways to OUTPUT a variable sent to a page using a FORM
#FORM.MyVariable#
#URL.MyVariable#
#MyVariable# (note: any locally defined variables will override this call)

So, Lets say we receive a LOGIN and a PASSWORD from an HTML Page as follows:

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF">
<FORM Action="CheckLogin.cfm" Method="Post"><br>

<p> enter your login name
<input type="text" name="login">
</p>
<p> enter your password
<input type="password" name="password">
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</FORM>
</body>
</html>

------

In the File: CheckLogin.cfm

We can access and output the Data within the Variables “#FORM.LOGIN# and #FORM.Password#”

CFML TAGS: Flow Control

IF I HAVE TO LEAVE RIGHT NOW

I AM GOING TO MISS THE BEST PART

ELSE

I AM GOING TO LEARN ENOUGH TO BE ABLE TO DO NEXT WEEKS ASSIGNMENT

END.

 

Flow control is very useful to help us analyse data submitted by a user

Remember our form example:

Typically when using a form you want the computer to:

1) Present the Problem: Ask the User their name…
2) WAIT for the user to submit their answer
3) Do something with the information

A SIMPLE LOGIN ALGORITHM:

Ask the User for their Login and Password

onSubmit: Check the Login and Check the Password against the right one, we can use coldfusion to do this.
onSuccess: Display a Welcome Message
onError: Display an Error Message and present the forms again

Since I am checking the value of the name and pasword after the submit button has been pressed on my form. I will evaluate its data in a second document called checklogin.cfm.

Since I am going to use coldfusion (cf) to evaluate the data, this document will have a .cfm extension.

You need to start by evaluating the entered data.

<CFIF Form.Login eq “Alex” AND Form.Password eq “MPM022”>

<FONT Size=“7”> WELCOME HOME!</font>

<CFELSE>

<FONT Color=“red”> Whoops, Try again!</font>

</CFIF>

Reading data from the db

After this, you can have coldfusion display particular type of data for the user.

 

First, you need to query the database for the information. Since our users has a password, use it to get a record

< CFQUERY Name=“GetData” Datasource=“nameofdb”>

SELECT First_Name, Last_Name

FROM students

WHERE password=#form.password#

</CFQUERY>

The pound sign is important here, it will allow us to use the value of the form field.... read up on it!!

 

ok. But all of this needs to work together

<CFIF Form.Login eq “Alex”>

< CFQUERY Name=“GetData” Datasource=“nameofdb”>

SELECT First_Name, Last_Name

FROM students

WHERE password='#form.password#'

</CFQUERY>

<cfoutput query="GetData">

<FONT Size=“7”> WELCOME HOME #first_name#!</font>

</cfoutput>

<CFELSE>

<FONT Color=“red”> Whoops, Try again!</font>

</CFIF>

For next week

BUILD A BASIC LOGIN FORM:
Here is what you will need.

1 – HTML File with Form Elements to ask your user a question
1 – CFML File with Flow {IF} to challenge the user’s answer

If the user’s answer matches your expected answer, then reward him, otherwise
Bounce or refer them back to the question page

REMEMBER: Work out the “FLOW” using Natural Language Syntax before you Touch a line of code.

Once you are confortable with reading from a db, how do get the user to enter data into it?