ColdFusion Tutorial

Creating a ColdFusion Poll system

This tutorial shows you an example how to create a poll system. Hopefully the tutorial will make it easy to finally add a poll on your site or perhaps it will provide you with the incentive to improve this tutorial. First some pictures, so you can see what this look like.


Some of the important poll features:
  • Write a cookie once the poll has been used so that the voter may not vote again.
  • Checks if the voter tries to use their browser back button to make multiple votes.
  • Check to see if the voter made a selection, if not the voter will be automatically redirected to the vote selection again.
  • Display the results for the poll using graphical bar as well as the numerical vote count and vote results as percentages.

Some important words about cookies:

An important thing to remember at all times is that cookies can be set to expire or a user can manually delete them whenever they want. There is no qyarantee that a cookie will exist. Ise the IsDefined function to check for existence of a cookie before attempting to recive the value of a cookie. This will prevent a template from throwing an error that is sometimes difficult to reproduze.


What we need for our poll application:
  • Create an Access database.
  • Create an image for the graphical bar in the poll
  • Create six coldfusion files:
    • application.cfm
    • app_global.cfm
    • index.cfm
    • header.cfm
    • bottom.cfm
    • cf_poll

Make a local test installation:

Create the directory structure:

Create a new directory in your directory: C:\Inetpub\wwwroot, name the directory cf_poll.

In the directory cf_poll crate two new directories named database and images.

Your directory structure should now look like this:

  • cf_poll [root]
    • ---database [directory]
    • ---images [directory]

Creating our database:

(In this example we are using MS Access).

Open Access and create a new database. Name the database my_poll and save it in the database directory. In the my_poll.mdb (Accesses) create two tables:

Questions

Field Name Data Type
QuestionID AutoNumber
Question Text

Answers

Field Name Data Type
AnswerID AutoNumber
QuestionID Number
Answer Text
Votes Number
Make QuestionID and AnswerID Primary Key. To easier enter data in our tables, create this relationship:

Enter the following data in the tables (open the Question table):

Question:

What is your favorite Editor?

(Click on the plus sign and enter Answer):

  • Dreamweaver
  • FrontPage
  • Visual Studio
  • Homesite
  • GoLive
  • NetObject Fusion
  • Text Editor
  • Others

Name the table "cf_poll" and save the database as "our_poll.mdb".

Open Coldfusion Server Administrator.
Select Data Sources
Type cf_poll in the Data Source Name field
Select Microsoft Access as Driver
and click on Add button.

Select Browse Server and find the our_poll.mdb.
Click on Apply, on the next screen click Submit.

If you have done everything correct you get the message datasource updated successfully.

Close ColdFusion Server Administrator.

Follow this link to see an animated tutorial how to add and manage your data source connections and Data Source Names (DSNs).


Images:

Copy those two images to the images directory (poll_img_1.gif and poll_img_2.gif). Those images are being used for the vote results graphic bar.

(On IE, right click and Save Picture As?)

poll_img_1.gif poll_img_2.gif
 
ColdFusion files:
 
application.cfm

<!--- Include variables from app_globals.cfm, so they are accessible in our application. --->

<cfinclude template="app_globals.cfm">

<CFAPPLICATION NAME="Cookies"

SessionManagement="Yes"

setDomainCookies="Yes">

 
app_globals.cfm

<!--- Application name --->

<!--- www.yoursite.com --->

<cfset application_name ="yoursite">

<!--- Database name: --->

<cfset request.main_access_DSN = "cf_poll">

<!--- Application running on server --->

<cfif CGI.REMOTE_HOST NEQ "127.0.0.1">

<cfset request.application.webroot = "http://#CGI.HTTP_HOST#">

<!--- Database user name: --->

<cfset request.dbusername = "username">

<!--- Database password: --->

<cfset request.dbpassword = "password">

<cfelse>

<!--- Application running on local server --->

<cfset request.application.webroot = "http://#CGI.HTTP_HOST#/#application_name#">

<!--- Database user name: --->

<cfset request.dbusername = "username">

<!--- Database password: --->

<cfset request.dbpassword = "password">

</cfif>

<!--- Always get the correct URL path --->

<cfset request.UrlStr ="http://"&#CGI.SERVER_NAME#&#CGI.PATH_INFO#>

 
header.cfm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<title>yoursite</title>

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

<style type="text/css">

<!--

body{

background-color: #669999;

margin-left: 0px;

margin-top: 0px;

margin-right: 0px;

margin-bottom: 0px;

}

.style1{

font-size: x-large;

font-weight: bold;

}

-->

</style></head>

<body>

<div align="center"><br>

<span class="style1">ColdFusion Poll</span> <br>

<br>

</div>

 

bottom.cfm

<div align="right"><br>

CopyRight&copy; yoursite [Applayit] </div>

</body>

</html>

 
index.cfm

<!--- Include our poll system: --->

<!--- Use: --->

<cfinclude template="cf_poll.cfm">

<!--- Or: --->

<cflocation url = "cf_poll.cfm">

 
cf_poll.cfm
I will try to improve my comments so this will be easier to understand. A complete download will also soon be available.

<!--- Include template header.cfm--->

<cfinclude template="header.cfm">

<!--- This example has only one questions table, so we set questions table to 1. --->

<cfset QuestionID = 1>

<!--- Set width of the vote and results template. --->

<cfset vote_width = 300>

<!--- Set graphic image for the results graphic bars. --->

<cfset vote_results_img = "poll_img_1.gif">

<!--- JavaScript for several submit buttons in on form. --->

<script language="javascript">

<!--

function changeFormAction(anAction)

{

document.Vote_Action.action = anAction;

document.Vote_Action.submit();

}

//-->

</script>

<!--- End JavaScript. --->

<!--- Case no action is defined set default action like vote. --->

<cfif Not IsDefined("action")>

<cfset action = "vote">

</cfif>

<!--- End </cfif> Not IsDefined("action"). --->

<!--- Evaluates a passed expression and passes control to the cfcase tag that matches the expression result. --->

<cfswitch expression = "#action#">

<!--- Expression value = vote --->

<cfcase value="vote">

Module: vote:

</cfcase>

<!--- End of the </cfcase> vote. --->

<!--- Show vote results. --->

<cfcase value="dsp_results">

Module: Display vote results.

</cfcase>

<!--- Find new vote results. --->

<cfcase value="act_results">

Module: New vote results.

</cfcase>

<!--- End </cfcase: new vote results. --->

<!--- In case of error, we use cfdefaultcase. --->

<cfdefaultcase>

<cfoutput>I received a action that I doesn't have a handler for. Please send mail to webmaster@applayit.com.</cfoutput>

</cfdefaultcase>

</cfswitch>

<!--- Include template bottom.cfm --->

<cfinclude template="bottom.cfm">

You find the code for the modules here.

ColdFusion Tutorial test

Creating a ColdFusion Poll system

No test is available or implemented for this ColdFusion Tutorial.

User Comments: 4

 cf_poll

I would really like to implement this poll, but I am having problems. There are two blocks of code for dsp_results. I removed one of the blocks, but it still does not work properly. I am not able to vote when I click "view results" when the poll first appears. I removed the commented out for the define cookies, once I thought I had everything working.

Please check to see if code is correct, since the dsp_results was coded twice.
It would be really nice once it is working.

Thanks for the tutorial

 Demo is available.

Yes, CF_Poll demo is available. I have now put a link to the demo on the top of the tutorial. The rest of the code (only one file left, will be available asap.)

Regards
Webmaster

 Demo?

Is a Demo available?

Thanks!

 where is the rest of the tutorial?

Is it coming soon?

Add your comments

Your Name*:
Your Email:
Site URL:
Site Name:
Comemt Title*:
Your Comment*:
Key Phrase*:
J3k
Your IP Address: 38.103.63.60
 

ScandicWeb is own by Scandic Systems LTD [UK] Company No. 5984000. All other trademarks and copyrights are the property of their respective holders.