Recent Comments

ColdFusion (CF)

Coldfusion is a scripting language that is used on web servers to merge web page output with optional content or results from a database or user input. Web pages by themselves are static, but intelligence placed on the web server through programs (like Coldfusion) allow a web page to change based on data it gets from the user or from other sources.

Download software: Coldfusion

Cost: Free

Instructions to setup software: http://www.learncfinaweek.com/week1/

Hardware requirements: PC, Mac

Operating systems supported: Win, Mac, Linux

Difficulty level: 4 out of 5


TRY IT!

1. Create a folder on your hard drive – call it “CF Demo”

2. Copy this Coldfusion and HTML code and paste into your favorite text editor. Windows notepad works fine.


<!---Coldfusion Document --->
<!--- Note: lines that start with the characters highlighted in yellow are comments - not code! --->
<cffunction name="convertTemperature"> 
	<cfargument name="temperature" type="numeric" required="true">
		<cfset returnText = "You converted " & #round(temperature)# & "#chr(186)#  F to " & #round((temperature - 32) / 1.8)# & "#chr(186)#  C"> 
		<cfif temperature LT 32>
			<cfset returnText = returnText & ": Pack Long Underwear">
		<cfelseif temperature GT 100 >
			<cfset returnText = returnText & ": Remember to Hydrate">
		</cfif>

		<cfreturn returnText>
</cffunction>

<cfoutput>	
<head> 
	<title>BSA Coldfusion Temperature Conversion</title> 
</head> 
<body> 
	<h2>BSA Coldfusion Temperature Conversion</h2> 
	<cfif isDefined("form.SubmitConvert")>
		<cfif isDefined("form.temperature") and Not val(form.temperature)>
			<strong>Please enter only numbers for the temperature</strong>	
		<cfelse>
			#convertTemperature(form.temperature)#
		</cfif>
		
	</cfif>
	<cfform name="form">
		<P>Enter a number in the box below to convert the temperature to Celcius.</P>
		Degrees: <input name="temperature" type="text" size=5>
		         <input name="SubmitConvert" type="submit">
	</cfform>
</body>
</cfoutput>

3. Save the File as “CF-Example.cfm” in the new folder you created in step 1. cfm as the extension stands for coldfusion markup. Other forms of Coldfusion extensions are cfc or cfml.

4. Double click on the CF-Example.cfm file to launch a browser. Enter a temperature and click the Submit Button to see the result. Try several different temperatures.


ABOUT THE PROGRAM — A WALK THROUGH

1. The HTML within the file defines the text and formatting information for a simple web page. Coldfusion and HTML are usually written together. HTML wraps the Coldfusion code. You can change anything you want to see how it affects the formatting of the page. This isn’t really programming, but it is fun to play with. It:

a. Creates a title for the website

b. Tells the HTML code to use Coldfusion and where to find the code

c. Formats the displayed text

d. Specifies a text entry box with ID = “temperature” (Coldfusion will need this)

e. Specifies a button labeled “Submit!”

f. When the mouse button is pressed, runs the CF convertTemperature function

g. Displays the string in “returnText” which is created in the CF function.

2. The CF form collects all the information that needs to be interpreted and sends the information to CF to be used.

a. Cf has its own set of input fields. One of them is the Submit type. When used it creates a button that submits the form.

3. Once the submit button is pressed, the form submits the information into the following block of code. It looks to see the function has been called, if not it calls the convertTemperature function with the cfinput variable. There is some basic error handling in the function to check that the input is a number.


<cfif isDefined("form.SubmitConvert")>
		<cfif isDefined("form.temperature") and Not val(form.temperature)>
			<strong>Please enter only numbers for the temperature</strong>	
		<cfelse>
			#convertTemperature(form.temperature)#
		</cfif>
</cfif>

4. The “convertTemperature ” function

a. Obtains the value entered in the “Input”-“Text” tag by using it’s ID: “temperature”

b. The value is converted to Celcius (with a single decimal place).

c. The Celcius value is evaluated with a series of IF Statements and a suggested action phrase is assigned to the variable: “returnText”

d. A sentence is created by combining or “concatenating” some static text along with the temperature values (deg F and Deg C) and the action phrase.

e. The new phrase is inserted onto the top of the HTML page the return variable from the function.


TRY THIS

1. Change the temperatures used in the decisions – change the lower temperature from 60 to 30 degrees, for example. Make sure you change it in two places! Save the file and refresh the browser (or restart the web page), and enter new numbers – did the answers change at the new temperature?

2. Create a new temperature range from 30 to 60 degrees and have it display – “Bring hat and gloves!”

3. Change the wording of the phrases

4. Take out the error handling and send in a letter. Leave only the part below.

	#convertTemperature(form.temperature)#

5. Add another text input – ask for the wind speed, for example

6. Add some conditional statements that evaluate the wind chill factor

7. Add some text to display the wind chill result


LEARN MORE

Adobe Coldfusion – This is Adobe’s resource for getting started with Coldfusion


RESOURCES, TIPS, TRICKS AND HINTS

Notepad++ is a great free text editor designed specifically for programmers. Check it out here.

Leave a Comment

Please do not use your real name.