Creating Web pages in Coldfusion
1. What is Application.cfm file in Coldfusion?
Application.cfm file is very important and special file for Web Application framework.
The basic function performed by this file is, the code of this file is automatically executed just before each of the pages that make up your application.
In simple words “The code of Application.cfm file will be automatically included just before any of your application pafes.” The behavior is the same as if you included the file using <cfinclud> tag.
So whenever a user visits a .cfm pade, coldfusion looks to see whether a file named Application.cfm exists in the same directory as the requested page. If so coldfusion automatically includes it.
If coldfusion could not found the file in the same folder then it looks in that folder’s parent folder. It will keep looking into these folders until there are no more parent folders to look into.
Sample Application.cfm file:
2. How to fetch records from database using coldfusion application?
3. How to make login page in coldfusion such that if the user is the first time user then he will be forwarded to some other page for example “profile creation page” and if the user is existing user then he will be forwarded to some different page for example “main page”?
Code:
Step 1: Create Application.cfm file.
Step 2: Create loginform.cfm
Copy and Paste this within <body> </body> tag.
Step 3: Create logincheck.cfm
Copy and paste this javascript within <head> </head> section
<script language="JavaScript" runat="server" type="text/javascript">
history.forward();
</script>
copy and paste within <body> </body> tag:
<cffile action="write" file="C:\track_login.txt" output="#form.userLogin#">
<cfif IsDefined("COOKIE.timeStart") EQ "NO">
<CFCOOKIE name="timeStart"
value="now()"
expires="now"></cfif>
<cfparam name="form.userLogin" type="string">
<cfparam name="form.userPassword" type="string">
<cfquery name="getUser" datasource="#request.datasource#">
select ID,LastName,FirstName,FirstTime from profile
where Username = '#form.userLogin#' and Password= '#form.userPassword#'
</cfquery>
<cfif getUser.recordcount eq 1>
<cfset session.auth = StructNew()>
<cfset session.auth.isLoggedIn = "1">
<cfset session.auth.id = getUser.ID>
<cfset session.auth.lastname = getUser.LastName>
<cfset session.auth.firstname = getUser.FirstName>
<cfif getUser.FirstTime eq 0>
<cflocation url="user_profile.cfm" addtoken="no">
<cfelse>
<cflocation url="name of url link to which you want to forward your visitor" addtoken="no">
</cfif>
</cfif>
<cflocation url="loginform.cfm" addtoken="no">
4. Change password web page using coldfusion?
Code:
step 1: change_pass.cfm
copy and paste below code in <head> </head> section
copy and paste below code in <body> </body> section
Step 2: passchange_proc.cfm
5. How to use ListBox in coldfusion? Or How to add values in ListBox in coldfusion?
How to add values in Listbox statically without fetching from database.
How to add values in Listbox dynamically, mean from database.
Application.cfm file is very important and special file for Web Application framework.
The basic function performed by this file is, the code of this file is automatically executed just before each of the pages that make up your application.
In simple words “The code of Application.cfm file will be automatically included just before any of your application pafes.” The behavior is the same as if you included the file using <cfinclud> tag.
So whenever a user visits a .cfm pade, coldfusion looks to see whether a file named Application.cfm exists in the same directory as the requested page. If so coldfusion automatically includes it.
If coldfusion could not found the file in the same folder then it looks in that folder’s parent folder. It will keep looking into these folders until there are no more parent folders to look into.
Sample Application.cfm file:
// Any variable set here can be used by all pages
// Start session management. Used to track session variables
<cfset this.sessionManagement = true>
// Start client management. Used to track client variables
<cfset this.clientmanagement = true>
<cfset this.name = "Name of your application">
<!--- On Request Start Function--->
// is called when user request any web page.
<cffunction name="onRequestStart" output="true" returntype="void">
<cfset request.datasource = "companyk1" >
<cfset request.companyname = "Company K1 - HQ">
/* call your header file everytime when user access webpage. So you do not have to include in every page*/
<cfinclude template="companyk1_header.cfm">
</cffunction>
/*On request end function is used to called end the end of webpage access */
<cffunction name="onRequestEnd" returntype="void" output="true">
<cfinclude template="companyk1_footer.cfm">
</cffunction>
// Start session management. Used to track session variables
<cfset this.sessionManagement = true>
// Start client management. Used to track client variables
<cfset this.clientmanagement = true>
<cfset this.name = "Name of your application">
<!--- On Request Start Function--->
// is called when user request any web page.
<cffunction name="onRequestStart" output="true" returntype="void">
<cfset request.datasource = "companyk1" >
<cfset request.companyname = "Company K1 - HQ">
/* call your header file everytime when user access webpage. So you do not have to include in every page*/
<cfinclude template="companyk1_header.cfm">
</cffunction>
/*On request end function is used to called end the end of webpage access */
<cffunction name="onRequestEnd" returntype="void" output="true">
<cfinclude template="companyk1_footer.cfm">
</cffunction>
2. How to fetch records from database using coldfusion application?
Assume: Table name = “profile” and it has ID, LastName, FirstName, FirstTime etc Column name.
Value of “FirstTime” field will be 0 or 1.
Value of “FirstTime” field will be 0 or 1.
/* Just before start of Body tag */
<cfquery name="getUser" datasource="#request.datasource#">
select ID, LastName, FirstName, FirstTime from profile
where Username = 'gaurang' and Password= 123456
</cfquery>
<body&gy;
<cfoutput query=”getUser”>
<table>
<tr>
<td>#getUser.ID#</td>
<td>#getUser.FirstName#</td>
<td>#getUser.LastName#</td>
<td>#getUser.FirstTime#</td>
</cfoutput>
</body&gy;
Result:
1 Gaurang Patel 0
<cfquery name="getUser" datasource="#request.datasource#">
select ID, LastName, FirstName, FirstTime from profile
where Username = 'gaurang' and Password= 123456
</cfquery>
<body&gy;
<cfoutput query=”getUser”>
<table>
<tr>
<td>#getUser.ID#</td>
<td>#getUser.FirstName#</td>
<td>#getUser.LastName#</td>
<td>#getUser.FirstTime#</td>
</cfoutput>
</body&gy;
Result:
1 Gaurang Patel 0
3. How to make login page in coldfusion such that if the user is the first time user then he will be forwarded to some other page for example “profile creation page” and if the user is existing user then he will be forwarded to some different page for example “main page”?
Code:
Step 1: Create Application.cfm file.
<cfset this.sessionManagement = true>
<cfset this.clientmanagement = true>
<cfset this.name = "BIOLOGY Department">
<cffunction name="onRequestStart" output="true" returntype="void">
<cfset request.datasource = "FlattenedFauna" >
<cfset request.companyname = "Company k1 - HQ">
<cfinclude template="Bio_header.cfm">
/* LoggedIn is a session variable which is used to restrict visitor from opening any page without login process. Below is the code to check that. If a user has pass through login process then “isLoggedIn” is true.
And if visior has not pass through login process and directly want to go to other pages then for that session “isLoggedIn” is not defined and so it is false and loginform.cfm page will open. */
<cfif not IsDefined("session.auth.isLoggedIn")>
<cfif IsDefined("form.userLogin")>
<cfinclude template="logincheck.cfm">
</cfif>
<cfinclude template="loginform.cfm">
<cfabort>
</cfif>
</cffunction>
<cffunction name="onRequestEnd" returntype="void" output="true">
<cfinclude template="Bio_footer.cfm">
</cffunction>
<cfset this.clientmanagement = true>
<cfset this.name = "BIOLOGY Department">
<cffunction name="onRequestStart" output="true" returntype="void">
<cfset request.datasource = "FlattenedFauna" >
<cfset request.companyname = "Company k1 - HQ">
<cfinclude template="Bio_header.cfm">
/* LoggedIn is a session variable which is used to restrict visitor from opening any page without login process. Below is the code to check that. If a user has pass through login process then “isLoggedIn” is true.
And if visior has not pass through login process and directly want to go to other pages then for that session “isLoggedIn” is not defined and so it is false and loginform.cfm page will open. */
<cfif not IsDefined("session.auth.isLoggedIn")>
<cfif IsDefined("form.userLogin")>
<cfinclude template="logincheck.cfm">
</cfif>
<cfinclude template="loginform.cfm">
<cfabort>
</cfif>
</cffunction>
<cffunction name="onRequestEnd" returntype="void" output="true">
<cfinclude template="Bio_footer.cfm">
</cffunction>
Step 2: Create loginform.cfm
Copy and Paste this within <body> </body> tag.
<cfform action="logincheck.cfm" name="LoginForm" method="post" >
<cfinput type="hidden" name="userLogin_required">
<cfinput type="hidden" name="userPassword_required">
<table border="0">
<tr><th colspan="2" bgcolor="#C0C0C0"><font face="Geneva, Arial, Helvetica, sans-serif" size="3" color="#003300">Please Log In </font></th></tr>
<tr>
<th><font face="Geneva, Arial, Helvetica, sans-serif" size="2" color="#003300">User Name :</font></th>
<td>
<cfinput type="text"
name="userLogin"
size="20"
value=""
maxlength="100"
required="yes"
message="Please Type your Username First"
style="border:1px solid ##000000; border-color:##000000; background-color:##FFFFCC" /></td>
<td>
<font face="Geneva, Arial, Helvetica, sans-serif" size="2" color="#003300">
Example : username@gmail.com</font></td>
</tr>
<tr>
<th><font face="Geneva, Arial, Helvetica, sans-serif" size="2" color="#003300">Password :</font></th>
<td>
<cfinput type="password"
name="userPassword"
size="12"
value=""
maxlength="100"
required="yes"
message="Please Type your Password First"
style="border:1px solid ##000000; border-color:##000000; background-color:##FFFFCC"/>
<cfinput name="btn_login" type="submit" value="Enter" style="border:1px solid ##000000; border-color:##000000;"/></td>
</tr>
</table>
</cfform>
<cfinput type="hidden" name="userLogin_required">
<cfinput type="hidden" name="userPassword_required">
<table border="0">
<tr><th colspan="2" bgcolor="#C0C0C0"><font face="Geneva, Arial, Helvetica, sans-serif" size="3" color="#003300">Please Log In </font></th></tr>
<tr>
<th><font face="Geneva, Arial, Helvetica, sans-serif" size="2" color="#003300">User Name :</font></th>
<td>
<cfinput type="text"
name="userLogin"
size="20"
value=""
maxlength="100"
required="yes"
message="Please Type your Username First"
style="border:1px solid ##000000; border-color:##000000; background-color:##FFFFCC" /></td>
<td>
<font face="Geneva, Arial, Helvetica, sans-serif" size="2" color="#003300">
Example : username@gmail.com</font></td>
</tr>
<tr>
<th><font face="Geneva, Arial, Helvetica, sans-serif" size="2" color="#003300">Password :</font></th>
<td>
<cfinput type="password"
name="userPassword"
size="12"
value=""
maxlength="100"
required="yes"
message="Please Type your Password First"
style="border:1px solid ##000000; border-color:##000000; background-color:##FFFFCC"/>
<cfinput name="btn_login" type="submit" value="Enter" style="border:1px solid ##000000; border-color:##000000;"/></td>
</tr>
</table>
</cfform>
Step 3: Create logincheck.cfm
Copy and paste this javascript within <head> </head> section
<script language="JavaScript" runat="server" type="text/javascript">
history.forward();
</script>
copy and paste within <body> </body> tag:
<cffile action="write" file="C:\track_login.txt" output="#form.userLogin#">
<cfif IsDefined("COOKIE.timeStart") EQ "NO">
<CFCOOKIE name="timeStart"
value="now()"
expires="now"></cfif>
<cfparam name="form.userLogin" type="string">
<cfparam name="form.userPassword" type="string">
<cfquery name="getUser" datasource="#request.datasource#">
select ID,LastName,FirstName,FirstTime from profile
where Username = '#form.userLogin#' and Password= '#form.userPassword#'
</cfquery>
<cfif getUser.recordcount eq 1>
<cfset session.auth = StructNew()>
<cfset session.auth.isLoggedIn = "1">
<cfset session.auth.id = getUser.ID>
<cfset session.auth.lastname = getUser.LastName>
<cfset session.auth.firstname = getUser.FirstName>
<cfif getUser.FirstTime eq 0>
<cflocation url="user_profile.cfm" addtoken="no">
<cfelse>
<cflocation url="name of url link to which you want to forward your visitor" addtoken="no">
</cfif>
</cfif>
<cflocation url="loginform.cfm" addtoken="no">
4. Change password web page using coldfusion?
Code:
step 1: change_pass.cfm
copy and paste below code in <head> </head> section
// This style is used to change background color when cursor moves on or moves out from button.
<STYLE>
.start {color:##006600; background:#999999;}
.end {color:##006600; background:##003300;}
</STYLE>
// Java script for changing background color.
<script type="text/javascript" >
function highlightButton(s)
{
// get the tagName for particular event. Here get input type of “BUTTON”. You can also use other input types instead of just button.
if ("INPUT"==event.srcElement.tagName)
event.srcElement.className=s // assign particular class style for particular event.
}
<STYLE>
.start {color:##006600; background:#999999;}
.end {color:##006600; background:##003300;}
</STYLE>
// Java script for changing background color.
<script type="text/javascript" >
function highlightButton(s)
{
// get the tagName for particular event. Here get input type of “BUTTON”. You can also use other input types instead of just button.
if ("INPUT"==event.srcElement.tagName)
event.srcElement.className=s // assign particular class style for particular event.
}
copy and paste below code in <body> </body> section
<cfform name="frm_pass_change" action="passchange_proc.cfm" method="post" onsubmit="return submitIt()">
<div align="center">
<table border="0">
<tr>
<td align="right">
Please Enter Your <font color="#003300" size="2" face="Verdana, Arial, Helvetica, sans-serif">Current</font> Password :</td>
<td>
<cfinput type="password" name="old_pass" style="border:1px solid ##000000; border-color:##000000; background-color:##FFFFCC" value="" size="20"/>
</td>
</tr>
<tr height="20">
</tr>
<tr>
<td align="right">
New Password:
</td>
<td>
<cfinput type="password" name="new_pass" style="border:1px solid ##000000; border-color:##000000" value="" size="20"/>
</td>
</tr>
<tr>
<td align="right">
Retype New Password :
</td>
<td>
<cfinput type="password" name="retype_pass" style="border:1px solid ##000000; border-color:##000000" value="" size="20"/>
</td>
</tr>
<tr height="20">
</tr>
<tr>
<td align="center" colspan="2">
<cfinput type="submit" value="Change It" name="btn_submit" style="border:1px solid ##333333; border-color:##000000;"
onMouseover="highlightButton('start')"
onMouseout="highlightButton('end')"/>
<cfinput type="reset" value="Reset" name="btn_reset" style="border:1px solid ##333333; border-color:##000000;"
onMouseover="highlightButton('start')"
onMouseout="highlightButton('end')"/>
</td>
</tr>
</table>
</td>
</tr>
<!--- Outer Tabel Ends here--->
</table>
</div>
</cfform>
<div align="center">
<table border="0">
<tr>
<td align="right">
Please Enter Your <font color="#003300" size="2" face="Verdana, Arial, Helvetica, sans-serif">Current</font> Password :</td>
<td>
<cfinput type="password" name="old_pass" style="border:1px solid ##000000; border-color:##000000; background-color:##FFFFCC" value="" size="20"/>
</td>
</tr>
<tr height="20">
</tr>
<tr>
<td align="right">
New Password:
</td>
<td>
<cfinput type="password" name="new_pass" style="border:1px solid ##000000; border-color:##000000" value="" size="20"/>
</td>
</tr>
<tr>
<td align="right">
Retype New Password :
</td>
<td>
<cfinput type="password" name="retype_pass" style="border:1px solid ##000000; border-color:##000000" value="" size="20"/>
</td>
</tr>
<tr height="20">
</tr>
<tr>
<td align="center" colspan="2">
<cfinput type="submit" value="Change It" name="btn_submit" style="border:1px solid ##333333; border-color:##000000;"
onMouseover="highlightButton('start')"
onMouseout="highlightButton('end')"/>
<cfinput type="reset" value="Reset" name="btn_reset" style="border:1px solid ##333333; border-color:##000000;"
onMouseover="highlightButton('start')"
onMouseout="highlightButton('end')"/>
</td>
</tr>
</table>
</td>
</tr>
<!--- Outer Tabel Ends here--->
</table>
</div>
</cfform>
Step 2: passchange_proc.cfm
// Paste this code before tag
// get your old password from profile table. Here #session.auth.firstname# and #session.auth.lastname# are the first and last name of user who is currently logged in.
<cfquery name="get_old_pass" datasource="#request.datasource#">
SELECT Password from profile where FirstName = '#session.auth.firstname#'
and LastName = '#session.auth.lastname#'
</cfquery>
// Update password if matched.
<cfif #form.old_pass# eq #get_old_pass.Password#>
<cfquery name="update_pass" datasource="#request.datasource#">
UPDATE profile
set Password = '#form.new_pass#'
where FirstName = '#session.auth.firstname#'
AND
LastName = '#session.auth.lastname#'
</cfquery>
<cflocation url="change_pass.cfm?pass_changed=1"/>
// Error message if current password is wrong.
<cfelse>
<cfoutput > <h1>Your Current Password is wrong.<br/>
Please Enter Correct Password.
<a href="change_pass.cfm">Try Again </a></h1></cfoutput>
</cfif>
// get your old password from profile table. Here #session.auth.firstname# and #session.auth.lastname# are the first and last name of user who is currently logged in.
<cfquery name="get_old_pass" datasource="#request.datasource#">
SELECT Password from profile where FirstName = '#session.auth.firstname#'
and LastName = '#session.auth.lastname#'
</cfquery>
// Update password if matched.
<cfif #form.old_pass# eq #get_old_pass.Password#>
<cfquery name="update_pass" datasource="#request.datasource#">
UPDATE profile
set Password = '#form.new_pass#'
where FirstName = '#session.auth.firstname#'
AND
LastName = '#session.auth.lastname#'
</cfquery>
<cflocation url="change_pass.cfm?pass_changed=1"/>
// Error message if current password is wrong.
<cfelse>
<cfoutput > <h1>Your Current Password is wrong.<br/>
Please Enter Correct Password.
<a href="change_pass.cfm">Try Again </a></h1></cfoutput>
</cfif>
5. How to use ListBox in coldfusion? Or How to add values in ListBox in coldfusion?
How to add values in Listbox statically without fetching from database.
<CFSELECT name="Species" width="100" style="border:1px solid ##000000; border-color:##000000; background-color:##FFFFCC" onChange="showimage()">
<option value="Deer">Deer</option>
<option value="Elk">Elk</option>
<option value="Bear">Bear</option>
<option value="Bighorn Sheep">Bighorn Sheep</option>
<option value="Coyote">Coyote</option>
</CFSELECT>
<option value="Deer">Deer</option>
<option value="Elk">Elk</option>
<option value="Bear">Bear</option>
<option value="Bighorn Sheep">Bighorn Sheep</option>
<option value="Coyote">Coyote</option>
</CFSELECT>
How to add values in Listbox dynamically, mean from database.
//table = "county_dist" Colunm name = "County"
<cfquery name="get_county" datasource="#request.datasource#">
SELECT DISTINCT County FROM county_dist
</cfquery>
<cfform name=”listbox”>
// value defines the name of column of table which values you want to use.
// display defines the name of column of table which values you want to display.
// multiple = “yes” allows you to select multiple values
<cfselect name="county" query="get_county" value="County" display="County" style="border:1px solid ##000000; background-color:##FFFFCC;" multiple="yes" size="3">
<option value="" selected="selected" >any</option>
</cfselect>
</cfform>
<cfquery name="get_county" datasource="#request.datasource#">
SELECT DISTINCT County FROM county_dist
</cfquery>
<cfform name=”listbox”>
// value defines the name of column of table which values you want to use.
// display defines the name of column of table which values you want to display.
// multiple = “yes” allows you to select multiple values
<cfselect name="county" query="get_county" value="County" display="County" style="border:1px solid ##000000; background-color:##FFFFCC;" multiple="yes" size="3">
<option value="" selected="selected" >any</option>
</cfselect>
</cfform>
/*More Topics are coming soon */
