Back to 2001 Conference Presentations
HighEdWeb Elections Using ColdFusion
Lucy Walker
SUNY New Paltz
Web Programmer and PC Support
Lucy@newpaltz.edu
Cold Fusion References
http://www.cfvault.com/ - some beginner/intermediate tutorials
http://www.houseoffusion.com/ - sign up for the CF-TALK mailing
list if you want to be inundated with email messages that are
sometimes very helpful. (You can also subscribe to digest mode
and get everything in just one message a day).
http://www.allaire.com/ - Support for
Cold Fusion is still found on the Allaire website, not Macromedia's. There is also
the Developer's Exchange where you can sometimes find code for free.
http://www.sys-con.com/coldfusion/ - Cold Fusion Developer's Journal.
Many great articles, you need to subscribe to the magazine to read all
of them but you can access a few without being a subscriber.
What is Cold Fusion?
- Software that runs on the web server
- .CFM files are processed by Cold Fusion server
- .CFM files contains ColdFusion Markuup Language (CFML)
What you need
- ColdFusion Server
- ColdFusion Studio - for editing .cfm files.
- Database
- Knowledge of SQL
Why we chose ColdFusion
- One person already knew it
- Easy for non-programmers to learn
- "Rapidly build and deploy database driven web pages"
Some of our CF Sites
- My.newpaltz.edu - students login once and have access to grades, schedules, transcripts, advisor information and phone bill
- Calendar of Events, News items, Staff phone directory, Job listings
ColdFusion Markup Language
- Consists of both tags and functions
- Tags start with <CF...
-
for example, <CFQUERY> tag is used to perform an SQL statement on a database
- Functions are things like Left(variable, #)
Database
- Can use MS Access, Oracle, SQL Server, MySQL, ...
- Link to database is setup in CF Administrator
Variables
- Syntax is name surrounded by #
- Values from a form - #form.fieldname#
- Values from url - #url.fieldname#
- Session , Client, Application and Server variables for storing a variable for a set of requests
- Variables that exists only in file, <CFSET CurrentDate = Now()>
HighEdWeb Elections
- Created MS Access database
- Form to nominate candidate
- Form to accept nomination
- Voting
- Assign code to each member
- Mail code and instructions to all members
- Forms to vote
HighEdWeb database
- Table of members - voting code, sign-in indicator, voted indicator
- Table of nominees
- Table for tabulation of votes
Assigning Login Codes
Get list of members - ID only:
SQL Select Statement Format
Select fieldname
>From tablename
Where criteria
SQL Update Statement
Update tablename
Set fieldname=value
Where criteria
CFOUTPUT
- <CFOUTPUT query="getID"> loops over all of the records returned by SQL
- To get value, use field name surrounded by # signs: #ID#
<CFOUTPUT query="getID">
#ID#
</CFOUTPUT>
<CFSET...> creates a variable and assigns a value to it
<CFSET code=RandRange(1,10000)>
Generates a random number between 1 and 10,000 and puts the value in #code#
Final Cold Fusion Code
<CFOUTPUT query="getID">
<CFSET code=RandRange(1,10000)>
<CFQUERY NAME="updatecode" DATASOURCE="highedweb">
UPDATE members
SET code=#code#
WHERE ID=#ID#
</CFQUERY>
</CFOUTPUT>
Mailing Codes to Members
Get member name, email addresses and codes:
<cfquery name="userquery" datasource="highedweb">
select name, email, code
from members
order by name
</cfquery>
<cfmail query="userquery"
to="#email#"
from="walkerl@newpaltz.edu"
subject="HighEdWeb Elections">
#name#,
Your code is #code#
</CFMAIL>
Voting
signin.cfm -> vote.cfm -> insertvote.cfm
Sign In to Vote (signing.cfm)
If the user does not enter a code, they will see the message in "value"
<form action="vote.cfm" method="post">
Enter your Election Code to vote:
<input type="Text" name="code" size=5>
<input type="hidden" name="code_required" size=5 value="You must enter a valid election code in order to cast your vote.">
<input type="submit" value="Sign In">
</form>
CFIF tag (signin.cfm)
<CFIF condition> code
<CFELSEIF condition> code
<CFELSE>
</CFIF>
Comparisons for condition are EQ, GT, LT, ...
Voting (vote.cfm)
<CFPARAM name="form.code" default="0">
<CFIF ISNumeric(form.code)>
<CFSET newcode=form.code>
<CFELSE> <CFSET newcode=0>
</CFIF>
Check Sign-in Code in Members table:
<CFQUERY NAME="checkcode" DATASOURCE="highedweb">
SELECT voted
FROM members
WHERE code=#newcode#
</CFQUERY>
Check if the code entered was found in the database: (nameofquery.recordcount)
<CFIF #checkcode.recordcount# IS 0>
The election code you entered is not valid. Please <a href="signin.cfm">try again</a>.
Check if this member already voted:
<CFELSEIF #checkcode.voted# NEQ 0>
The election code you entered has been used already. Try entering your <a href="signin.cfm">code again</a>
Mark as Signed In (vote.cfm)
Mark this member as having signed in:
<CFELSE>
<CFQUERY NAME="marksignedin" DATASOURCE="highedweb">
UPDATE members
SET loggedin=-1
WHERE code=#form.code#
</CFQUERY>
Getting List of Nominees (vote.cfm)
<cfquery name="showprez" datasource="highedweb">
SELECT *
FROM nominations
ORDER BY position, nominee asc
</cfquery>
Form fields for voting (vote.cfm)
<cfoutput query="showprez" group="position">
#Position#
<cfoutput><input type="radio" name="#position" value="#nominee#"> #nominee# (#university#)
</cfoutput>
</cfoutput>
Insert Vote (insertvote.cfm)
<CFINSERT> tag (insertvote.cfm)
If the names of the form fields match the names of the fields in the database, you can use CFINSERT instead of <CFQUERY> with an SQL Insert statement
<cfinsert datasource="highedweb"
tablename="elections"
formfields="president, vice_president, secretary, treasurer, president_other, vp_other, secretary_other, treasurer_other">
NOTE: There is an article on the Macromedia(Allaire) website that recommends
you DON'T use cfinsert.
Mark member as "voted"
<CFQUERY NAME="markvoted" DATASOURCE="highedweb">
UPDATE members
SET voted=-1
WHERE code=#form.code#
</CFQUERY>