Internet.comReal World Active Server Pages Solutions and Resources 
ASPWatch.com
Sponsored by Compuware Corporation
Essential survival skills for Internet success!
ScriptingDatabaseComponentsXMLIntegrationSolutions

Newsletter
Click here!
Search


Pete Nelson's Home Page
Log off

internet.com
Internet News
Internet Investing
Internet Technology
Windows Internet Tech.
Linux/Open Source
Web Developer
ECommerce/Marketing
ISP Resources
ASP Resources
Wireless Internet
Downloads
Internet Resources
Internet Lists
International
EarthWeb
Career Resources

Search internet.com
Advertise
Corporate Info
Newsletters
E-mail Offers


Image & Video Software
E-Business Exchange
Manage Your E-Store
Buy BSD Products
Buy Computer Products
Internet Intelligence
IT Solutions
Add Chat To My Site
Hosting Services
Outsource IT Work

Quick and Dirty Form Processing
by Pete Nelson
Skill level: Beginner
First posted: Wednesday, December 08, 1999
Quick and Dirty Form Processing

With the amount of code I've written over the past couple years, I often grow tired of writing whole ASP pages just to process each field on a form, especially if there's a lot of fields. The following is some code you can use to speed up the creation (and later modification) of a form and the code used to process the data on that form. In this case, we'll be saving the data to a table using ADO.

Setting Up the Form

One of the tricks here is to make sure each field in the form has a corresponding field in the table. Let's say we have an SQL table that has three fields - Email, FirstName and LastName. In the form below, use these same field names, preceded by "_F_" (which I'll explain a bit later).

<form name="Users" method="post" action="xt_save_user.asa">
EMail: <input type="text" name="_F_Email" size="20" maxlength="50"><br>
First Name: <input type="text" name="_F_FirstName" size="20" maxlength="50"><br>
Last Name: <input type="text" name="_F_LastName" size="20" maxlength="50"><br>
<input type="submit" value="Save">



Your Friendly ASP Code

In our xt_save_user.asa file, we parse through all the fields in the Request collection. If the field name starts with "_F_", then we can assume it corresponds to a field name in the SQL table. We trim off the "_F_", assign the field name in SQL the value from the corresponding field name in the form.

<!-- #include file="adovbs.inc" -->

<%
Set con = Server.CreateObject("ADODB.Connection")
con.Open "DSN=Users"

Set rec = Server.CreateObject("ADODB.Recordset")
rec.Source = "select * from Users where EMail = '" & Request("_F_EMail") & "'"
rec.ActiveConnection = con
rec.Open , , adOpenKeyset, adLockOptimistic

if rec.BOF and rec.EOF then
   rec.AddNew
end if

for each item in Request.Form
   if instr(item, "_F_") = 1 then '*** This is a database field
      strFieldName = Replace(item, "_F_", "") '*** Remove the _F_
      rec(strFieldName) = request(item) '*** Set the field name to the form value
   end if
next

rec.Update
rec.Close

Set rec = Nothing
%>


Wrap-Up

As you can see, we can now add additional fields to the form and the SQL table without having to modify the ASP code that processes those fields. This is just a small sample of what you can do. With some extra smarts built-in, the code can put the data into multiple tables. By varying the field name, you could have the code do simple validation. Lots of possibilites with this one!

Pete Nelson
Pete is the webmaster for DVD Tracker, a site that lets people keep an online catalog of their DVD collection. He has been doing ASP development for three years and is proficient in VBScript, SQL and HTML, and dabbles in VB COM, PHP and mySql now and then. He's married to a Linux geek and lives in the San Francisco Bay Area.
What did you think of this article?
Not usefulVery useful
Badly writtenWell written
Too shortToo long
This document can be found in these Encyclopedia chapters:
º Tricks of the Trade
This is what people have been saying about this article:
yest another hack - Anonymous - Thursday June 8 12:10:00 PM
all uppercase form items? - Anonymous - Wednesday February 23 9:48:00 AM
bug in code - Anonymous - Wednesday July 12 8:54:00 AM
   bug in code - Anonymous - Monday February 21 8:42:00 PM
   bug in code - Anonymous - Wednesday July 12 8:54:00 AM

Create a new discussion
View Article Statistics

Authors...
Edit this article
View Preview Version
Printable Copy of Article
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Essential survival skills for Internet success!

About your privacy | Want to advertise? | Contact Us

Copyright © internet.com Corporation 2001
http://www.internet.com