Inserting data into a database
This tutorial will show you how to insert
data into your database by using ColdFusion Markup Language.
The first thing I will explain is that you will
have a form page and an action page. The form page is the page where the
user will type in the data and the action page is where ColdFusion will insert
the data into the database.
Let's begin by making the form page.
Create a new page and call it "form.cfm".
Then type in this code on it.
<HTML>
<HEAD>
<TITLE>This is the form
page</TITLE>
</HEAD>
<BODY>
<FORM
ACTION="action.cfm"
METHOD="post">
<input type="text"
name="firstname"
value="">
- First Name<BR>
<input type="text"
name="lastname"
value="">
- Last Name<BR>
<input type="text"
name="email"
value="">
- Email<BR>
<textarea name="message"
rows="7"
cols="40"></textarea><BR>
<font size="1" face="verdana">Additional
Message</font>
<input type="submit"
name="Submit"
value="">
</FORM>
</BODY>
</HTML>
The code above will create a form that will
allow a visitor to type in their first name, last name, email address and a
brief message to you. Think of it as a contact form so your customers can send
you an email to ask you a question. When the user clicks on the
"submit" button, they will then be directed to the "action.cfm"
page with all of the information they have typed in. The "action.cfm"
is the page that will use ColdFusion server to process the information, save it
to a database and then send you an email copy of the visitors submission.
Create a new page and call it "action.cfm"
The "action.cfm" page consists of
both regular HTML code and ColdFusion processing code. here's how this page is
made:
<HTML>
<HEAD>
<CFOUTPUT>
<TITLE>Thank
you #firstName# #Lastname# for your submission!</TITLE>
</CFOUTPUT>
</HEAD>
<BODY>
<!--- First let's insert this data into our database for safe keeping and
later usage. --->
<CFINSERT DATASOURCE="YourDSN"
TABLENAME="TableName"
FORMFIELDS="firstname, lastname, email,
message">
<!---
Now let's send a copy of this submission by email to ourselves --->
<CFMAIL FROM="#form.email#"
TO="youremail@yourisp.com"
SUBJECT="Submission from website!"
SERVER="mail.yoursite.com"
PORT="25">
There has been a form submission on your site,
here's what they had to say:
Sender name: #form.firstname#
#form.lastName#
Sender email: #form.email#
Sender IP: #REMOTE_ADDR#
Message: #Message#
Message Sent: #DateFormat(now(), 'mmmm dd, yyyy')#
#TimeFormat(Now(), 'hh:mm:ss tt')#
</CFMAIL>
<!--- Now Let's Send An
Email To The Person Submitting This Form, Thanking Them For The Form Submission
--->
<CFMAIL FROM="youremail@yourisp.com"
TO="#form.email#"
SUBJECT="Thank you for submitting our
form!" SERVER="mail.yoursite.com"
PORT="25">
Dear #form.firstname# #form.Lastname#,
Thank you for
submitting Our Contact Form. We will contact you back within 24 hours. We've
also
sent you a copy of what you submitted below.
Thanks again,
Your Name
Your Company
Submission
Receipt:
===========================================================
Sender name: #form.firstname# #form.lastName#
Sender email: #form.email#
Sender IP: #REMOTE_ADDR#
Message: #Message#
Message Sent: #DateFormat(now(), 'mmmm dd, yyyy')#
#TimeFormat(Now(), 'hh:mm:ss tt')#
</CFMAIL>
<!---
Now we actually have to display something on the page to let the user know that
his submission was successful --->
<CFOUTPUT>
Thank you #form.firstname# #form.lastname#, we have
successfully received your request and it's been sent to the coresponding
support personnel. Expect to hear from us within
24 hours. A copy of this form submission was also sent to the email
address you specified as being
yours in the previous page for your records.<BR>
<BR>
Thanks,<BR>
Your Name<BR>
Your Company
</CFOUTPUT>
</BODY>
</HTML>
Now, let me explain what the code above does in
detail.
First notice that on the heading section of the page, right before and after the
<title></title> tags, I put a <CFOUTPUT>
tag, this was done so that you could customize this users experience by putting
their name on the tile of the page. Keep in mind that anything between the
<CFOUTPUT> tags with # around it will be
processed and a value will be returned, in this occasion it was the person's
first and last name.
The next thing I'd like you to notice is the
tag <CFINSERT>, this tag is a ColdFusion
internal processing tag that allows you to insert the data into your database
much easier and faster then the traditional way of using <CFQUERY>.
The tag requires a few values to be set for it to correctly function. Here is
what they are:
DATASOURCE="YourDSN" - the datasource is the name of your ODBC
connection to your database. If you are unsure of this, please consult the
ColdFusion documentation.
TABLENAME="TableName" - This is the name of the table that you
will be using to insert the records into. You can name it anything you'd like,
but it must be in the database that is mapped with the datasource specified
above.
FORMFIELDS="firstname, lastname, email, message" - This is the
field in the form that you actually want to input into the database. This
is optional, but highly recommended that you use. The reason being is that if
you would have named the submit tag to "SendButton" and there was no
field in your table specified above called "SendButton" you'd receive
an error. So please always use the "formfields" option to
prevent errors.
The next things we did was use the <CFMAIL>
tag. This is a tag that tells ColdFusion that you want to send an email
message from your site.
This tag requires a few value also as you noticed above. Here is what those
values are:
FROM - This is the field that let's ColdFusion know who the sender of the
email is, in case the person receiving the email wants to reply to this email.
TO - This is the field that let's ColdFusion know where the email is to
be delivered.
SUBJECT - This is the subject of the message.
SERVER - This is the mail server you'd like to use to send the email.
This is also an optional field, but highly recommended that you use.
PORT - This is the port that the mail server specified above is listening
on to send the email. This field is optional, but becomes required when you
specify a SERVER.
A few functions I used within the form are:
#remote_addr# - this is to get the users IP address and include it on
your email.
#DateFormat# - The date format function allows you to specify and format
the date according to your needs. In this case we're using the format:
mmmm dd, yyyy which translates to: May 1, 2001.
#TimeFormat# - This allows you to take the current time and format it
according to your needs. In this case we used the format:
hh:mm:ss tt which translates to: 3:35:23 PM
now() - this was specified within the DateFormat and TimeFormat tags
above, it returns the current date/time for formatting with the tags above.
The last tag we've used is the <CFOUTPUT>
tag. Like I mentioned before, this is the tag that ColdFusion uses to process
values. Without this tag, the field "form.firstname# would not get
processed and would therefore be displayed as #form.firstname# instead of
"bob".
NOTE (for your information, not related to tutorial):
A question I always get is:
If ColdFusion needs <CFOUTPUT> to process
values, why is it that the <CFMAIL> and the <CFUPDATE>
do not need to be within the <CFOUTPUT> tag?
My answer always is:
Some ColdFusion tags such as <CFMAIL> and <CFINSERT>
have capabilities of executing <CFOUTPUT>
type actions. Think of them as having a built-in <CFOUTPUT>
in their processing system. Therefore they do not need to have <CFOUTPUT>'s
around them to process for values!
You have successfully created a submit form that inserts data into a database
and then sends an email to you and your visitor at the same time.
-
Inserting data into a database
This tutorial will show you how to insert data into a database, then have that information emailed to you and the person submitting the data.
Author: Pablo Varando
Views: 56,042
Posted Date: Thursday, August 1, 2002
-
Retrieving Records From a Database..
This is the basics of ColdFusion. This tutorial will demonstrate how to query a database and then display the records found.
Author: Pablo Varando
Views: 49,193
Posted Date: Saturday, August 3, 2002
-
Having Your Database Do The Work… not ColdFusion!
This tutorial will demonstrate how you can use the functions that come built in on your database to do the work, instead of doing the work in your code the hard way!
Author: Pablo Varando
Views: 44,001
Posted Date: Thursday, August 8, 2002
-
A Simple Contact Us Page….
Learn how to create a contact page in ColdFusion.
Author: Pablo Varando
Views: 54,243
Posted Date: Tuesday, August 13, 2002
-
DSNLess Coldfusion?
Learn how to create database connection, by skipping the old ODBC connections with ColdFusion.
Author: Pablo Varando
Views: 40,902
Posted Date: Friday, August 16, 2002
-
Creating a user athentication (Login) area.
This tutorial will demonstrate how you can create a "member's only" area. It will show you how to log them in and how to check that they are logged in.
Author: Pablo Varando
Views: 114,157
Posted Date: Monday, August 19, 2002
-
User Defined Functions....
Learn how to use User-Defined Functions in ColdFusion 5.0.
Author: Pablo Varando
Views: 33,375
Posted Date: Wednesday, August 21, 2002
-
Count Active Users On Your Site.
Have you ever wanted to display a count of how many people are on your web site at any given moment? This tutorial will demonstrate to you how to achieve just that. It will count your web site's active sessions and allow you to display them to your visitors.
Author: Pablo Varando
Views: 48,400
Posted Date: Sunday, August 25, 2002
-
Creating a Newsletter System....
This tutorial will show you how to create a fully automated system to allow visitors to subscribe and unsubscribe to your newsletter, and for administrators to send out a newsletter to all the registered users.
Author: Pablo Varando
Views: 46,577
Posted Date: Friday, September 6, 2002
-
A brief demonstration of Fusebox 2.0
This is a brief demonstration on how to use Fusebox 2.0 Methodology.
Author: Pablo Varando
Views: 36,531
Posted Date: Friday, September 6, 2002
-
A quick intro into the world of Custom Tags!
The following tutorial will briefly touch over Custom Tags and show you what they are, how you use them, and how they benfit you by using them.
Author: Pablo Varando
Views: 38,432
Posted Date: Friday, September 6, 2002
-
Using Query String Values....
This tutorial will demonstrate how to use query string values instead of form values.
Author: Pablo Varando
Views: 57,933
Posted Date: Sunday, September 15, 2002
-
Previous / Next n Records
This tutorial demonstrate how to implement "Previous" & "Next" into your existing results page.
Author: Pablo Varando
Views: 52,664
Posted Date: Tuesday, September 17, 2002
-
Alternating Row Colors!
This tutorial will demonstrate how to alternate row colors when outputing your data.
Author: Pablo Varando
Views: 61,339
Posted Date: Tuesday, September 17, 2002
-
Using PayPal's IPN with ColdFusion!
This tutorial will demonstrate how to implement the PayPal IPN (Instant Payment Notification) into your e-commerce applications to accept credit cards in real time!
Author: Pablo Varando
Views: 92,047
Posted Date: Wednesday, September 25, 2002
-
Clearing your session variables!
This tutorial will demonstrate how to clear your applications sessions variables with just three lines of code!
Author: Pablo Varando
Views: 51,514
Posted Date: Friday, October 4, 2002
-
ColdFusion and .INI Files!
This tutorial will demonstrate how to use .INI files with ColdFusion. Perfect for users wishing to create administration areas for existing software titles that are INI file driven (i.e. FTP Servers).
Author: Pablo Varando
Views: 38,691
Posted Date: Friday, October 4, 2002
-
Sending multiple attachments with CFMAIL!
This tutorial will demonstrate how to send out multiple attachments with .
Author: Pablo Varando
Views: 57,938
Posted Date: Friday, October 11, 2002
-
Creating, Altering and Deleting database tables with ColdFusion.
This tutorial will show you how to create, modify and delete database tables easily with ColdFusion.
Author: Pablo Varando
Views: 45,368
Posted Date: Monday, October 14, 2002
-
Inserting FORM data into multiple database tables!
This tutorial will demonstrate how you can use one form a user fills out to insert into multiple database tables.
Author: Pablo Varando
Views: 38,011
Posted Date: Tuesday, October 15, 2002
-
Implementing FORM Error Checking On Your Pages!
This tutorial will show you two two ways you can implement error checking, to ensure that your users are actually entering the required fields on your forms!
Author: Pablo Varando
Views: 35,794
Posted Date: Wednesday, October 16, 2002
-
Using Arrays in ColdFusion To Properly Display Data....
This tutorial will show you how to use arrays to display data properly in ColdFusion.
Author: Pablo Varando
Views: 43,087
Posted Date: Monday, October 28, 2002
-
Automatically Adding Smiles To Your Messages!
This tutorial will show you how you can add smiles to your messages on the fly!
Author: Pablo Varando
Views: 35,821
Posted Date: Tuesday, October 29, 2002
-
Reading your IIS Log Files with ColdFusion!
This tutorial will show you how you can parse through your IIS (web server) log files and insert the values into a database, therefore allowin you to display real-time stats to your visitors (i.e hits this week, etc..)
Author: Pablo Varando
Views: 45,460
Posted Date: Monday, November 4, 2002
-
Using CFRegistry to Add Your IP To CF Debug IP List!
This tutorial is intended to show you how to use the ColdFusion tag <CFRegistry>. This tutorial will show you how to add your current IP to the IP Debug List in the ColdFusion Administrator.
Author: Pablo Varando
Views: 49,516
Posted Date: Wednesday, November 6, 2002
-
Using <CFPOP> and creating an email client for POP3 Email Reading!
This tutorial will show you how to create a mail system for your site. It will allow you to get your email from a POP3 server, view your inbox, then view the message (with attachments), reply and delete that message as well.
Author: Pablo Varando
Views: 49,746
Posted Date: Thursday, November 7, 2002
-
Print your web pages on the fly!
This tutorial will demonstrate how use ColdFusion, Javascript and Style sheets to create the perfect Printing Machine! ;)
Author: Pablo Varando
Views: 40,539
Posted Date: Sunday, December 15, 2002
-
Creating an ODBC Connection within ColdFusion MX Server...
This tutorial will show you how to create an ODBC (Database) connection from within your ColdFusion MX Administration Area.
Author: Pablo Varando
Views: 51,199
Posted Date: Monday, January 6, 2003
-
CaSe SensitiVe password logins!
This tutorial will demonstrate how to verify users passwords to be CaSe SensiTive so add another layer of security to your applications!
Author: Pablo Varando
Views: 75,059
Posted Date: Wednesday, February 5, 2003
-
Combining two queries into one..
This tutorial will demonstrate how to create a query from two different queries based from two separate datasources. This is the easiest way to combine your data.
Author: Pablo Varando
Views: 39,155
Posted Date: Monday, March 10, 2003
-
Preventing People From Leeching Your Images!
This tutorial will show you how to load your images from an actual .cfm page. Therefore, allowing you to prevent people from using your content on their web sites.
Author: Pablo Varando
Views: 47,749
Posted Date: Saturday, August 28, 2010
-
Get A Folder Size Using ColdFusion and FSO...
This tutorial will demonstrate how you can get the size of a folder (and sub folders) using ColdFusion and Windows File System Object (FSO).
Author: Pablo Varando
Views: 37,612
Posted Date: Tuesday, April 8, 2003
-
Do you want to remember your members?
This tutorial will show you how to you can provide your members with the ability to save their username and password into memory, so they dont have to type it in everytime the want to log in to your web site.
Author: Pablo Varando
Views: 39,383
Posted Date: Tuesday, May 13, 2003
-
Delete Records From Your Database With ColdFusion!
This tutorial will demonstrate how to delete records from a database via your website using ColdFusion.
Author: Pablo Varando
Views: 42,174
Posted Date: Friday, July 4, 2003
-
Creating a file content crawler with ColdFusion....
This tutorial will show you how to make a file content crawler with ColdFusion to find specified documents in a folder and its children folders. (Similar to find files or folder in Windows(c) Operating Systems 'find' feature).
Author: Pablo Varando
Views: 50,169
Posted Date: Saturday, July 19, 2003
-
What is the ID for the record I just inserted?
This tutorial will demonstrate how you can get the ID of the record you have just inserted without having to connect to the database again!
Author: Pablo Varando
Views: 37,610
Posted Date: Monday, August 11, 2003
-
Changing the form submission page on the fly!
This tutorial is not ColdFusion oriented, but covers a great trick to allow you to submit a single form to a variety of different pages on the fly.
Author: Pablo Varando
Views: 29,811
Posted Date: Monday, December 1, 2003
-
Processing XML/RSS feeds with ColdFusion MX
This tutorial will show you how to parse XML files (RSS Feeds) with ColdFusion MX and it uses an EasyCFM.COM Feed for example [Feed: 5 Most Viewed Tutorials]. It shows you how to call it via CFHTTP all the way to parse and display your records!
Author: Pablo Varando
Views: 59,472
Posted Date: Saturday, December 27, 2003
-
Creating your very own RSS XML Feeds with ColdFusion MX!
Have you ever wanted to create your very own RSS XML News Feeds? This tutorial will show you how to create an RSS feed that will allow you to syndicate your web site and allow the world to easily use your data!
Author: Pablo Varando
Views: 68,490
Posted Date: Thursday, January 15, 2004
-
Correct Content (document) serving!
This tutorial will demonstrate how to correctly serve documents via ColdFusion and allow you to correctly name the download as you see fit!
Author: Pablo Varando
Views: 28,251
Posted Date: Tuesday, February 10, 2004
-
Dynamic Last Date Modified?
This tutorial will demonstrate how to display the date a web page was last modified to your visitors dynamically.
Author: Pablo Varando
Views: 27,827
Posted Date: Monday, April 12, 2004
-
Delete files and folders in a specified path!
This tutorial will demonstrate how you can delete all files and sub-folders in a specified folder using ColdFusion and Windows!
Author: Pablo Varando
Views: 33,267
Posted Date: Wednesday, September 7, 2005
-
The best way to show categories (and their children, and their children and those children)
Today I ran by a forum post on EasyCFM.COM (see it here) that asked for help in displaying categories. This can be tricky; especially if you have many parent/child relationships. So I thought I would post a blog entry talking about the best way I have found to do this... Here goes nothing..
Author: Pablo Varando
Views: 7,269
Posted Date: Saturday, December 6, 2008
-
ColdFusion User Interfaces - Series (CFMENU)
This new tutorial series will show you how to create user interfaces with ColdFusion MX 8!
Author: Pablo Varando
Views: 5,842
Posted Date: Wednesday, May 20, 2009
-
Generating random colors on your ColdFusion Charts using a User Defined Field (UDF)....
This tutorial will show you how to efficiently re-use code and in addition how to create a random list of colors that you can use with cfchart colorlist.
Author: Pablo Varando
Views: 3,937
Posted Date: Friday, June 5, 2009
-
Annonymous User - Unread Article Notification Tracking
Have you ever wanted to let your annonymous (non logged in / members) users know what items they have not yet read in your blog or discussion board / forum? This tutorial shows you a real easy and quick way to do just that... take a look now!
Author: Pablo Varando
Views: 2,860
Posted Date: Saturday, October 10, 2009