Electric Type

Multimedia

About Us

News

Help

Cool Forms with ColdFusion

Page 2 — Data Aware of Your Data

I've been out of college for a while, and now I'm trying to get rid of some of my old textbooks. I have a few copies of each book, and I want to give them away on a first-come, first-served basis. Since I don't care to spend money on a newspaper ad, I decided to make the offer on my Web site.

First, I have to create the sign-up form. I opened up Notepad and created a form that looks like this:

Title Copies Choice
The Art of War 4
The Campaigns of Alexander 1
The Conquest of Gaul 3
The Unabomber's Manifesto 2

Name:
Address:
City:
State:
Zip:
Phone:

It's simple. The user selects a book, enters his or her contact information, and submits the form. The form gets handed off to a CGI script that I ripped off the Net, and I receive an email telling me who wants what. I drop the book in the mail, some lucky person gets it, and everyone is happy.

But what happens if a lot of people start asking all at once or I run out of a book? I'm not going to change this form by hand every time someone orders something. I need to make this form data-aware by tying it to a database. Every time someone signs up for a book, I'll have one less book available. When I run out of a book, the form will reflect that.

I've created a simple database, which you can download here. In it, I have the following inventory table, which contains the information that we are going to use in our form. If you have ColdFusion Server loaded at home and you wish to play along, you'll need to set up a data source name (DSN) for this database so ColdFusion can find it.
Inventory
IDtitleinstock
1The Art of War4
2The Campaigns of Alexander1
3The Conquest of Gaul3
4The Unabomber's Manifesto2

With your DSN set up, fire up ColdFusion Studio and open up the form we created (it should look like this). We want to take information out of the database and put it into our form. So at the top of your form, insert a <CFQUERY>. This is what mine looks like:

	<cfquery name="Inventory" datasource="wired_cf">		select id, title, instock from inventory
</cfquery>

This retrieves all of the information out of the inventory table.

If this doesn't make any sense to you, go ahead and read Charles' tutorial.

Now that we have the information, we need to do something with it. This is where ColdFusion shines. The first step is for us to locate the <FORM> tag at the top of our form and change it to a <CFFORM>. This is ColdFusion's custom form tag, and if we use it in our application, good things will happen, as you'll see down the road.

Now that we have converted our old form to a CF form, we need to make it data aware. This is how I want it to work: If a book is available, the user should have the option to select it. But if there is no book available, the form should say, "All gone."

To do this, we are going to loop through the query that we created using the <CFOUTPUT> tag. We'll add to that a very simple If ... Else statement to check if there are any books in stock. The code will look like this:

<cfif instock gt 0>		<input type="radio" value="#id#" name="choice"><cfelse>	All gone.</cfif>

At this point, we have a working, data-aware form. Take a second to look it over and play with it. Change some info in the database. Set some of the values to zero to make sure that everything works. When you come back, we'll work on step 2: validation.

next page»


Tutorials  

User Blogs  

Teaching Tools  

Authoring  

Design  

Programming help  

Advanced Flash  

Javascript  

Glossary  

PHP Coding  

User Blogs

Screen Shots

Latest Updates

Contact Us

Valid HTML 4.01!
Valid CSS!

Breadcrumb

© ElectricType
Maintained by My-Hosts.com
Site map | Copyright | Disclaimer
Privacy policy | Acceptable Use Policy
Legal information.