|
|
| Databases 101 | | | ASP.NET Chapter | | | Suggest Article | | | Discussions | | | Bookstore | | | Write for Us | | | OpenAuction | | | Our Authors | |
|
|
|
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!
|
| |  | |  |
This document can be found in these Encyclopedia chapters: º Tricks of the Trade
|
 |
|
 |
This is what people have been saying about this article:
Create a new discussion |
 |
|
 |
View Article Statistics
Authors... Edit this article View Preview Version
|
|
| Printable Copy of Article |
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
|
|