HAL-PC Web Technolgies SIG

Mail List | SIG Leader

Forms to Database

HAL-PC Web Technologies SIG

Retrieving Form Information and Saving to a Database

Create a form

A simple form used in this presentation will add user information to the database to keep track of user comments:

Form Code

Code for this form is:
<form action="asp_db1.asp" method="post" name=“comments" id="comments">
<p><label for="vname">Name:</label>
<input type="text" name="vname" id="vname" />
<br />
<label for="email">Email:</label>
<input type="text" name="email" id="email" />
<br />
<label for="comment">Comment:</label>
<textarea name="comment" cols="40" rows="8" id="comment"></textarea></p>
<p class="ctr">input type="submit" name="Submit" value="Submit" />
&nbsp;<input type="reset" name="Reset" value="Clear" /></p>
</form>

Pass The From Data

To retrieve the information from a form field you can use either of the following methods:
Get method: request.querystring(“field”)
Post method: request.form(“field”)

Validation Options

There are times when you want to require that a field have information before being submitted. Form fields can be validated in one of two ways:
Client Side with javascript
Server Side with ASP

javascript Validation

In most case using javascript to validate client side is preferred but if a field really must be completed it is best to validate server side as well to catch those who do not have javascript support available on their browser.

For this session no validation will be used.

Retrieve with ASP

In the original form there are 3 form fields:
Name = vname
Password = password
Comments = comment
We will dim variables for each field:
Dim vname, email, comment

Database Connection

The connection string used here is:
sConnString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\hshome\designsi\database\forms.mdb“
One reason for using the full path is when the database is outside the root of your domain and the web host has disabled relative paths for security reasons. If the database is available using a relative path then you can use server.mappath
<% ' ASP server-side code oConn.Open "Driver={Microsoft Access Driver (*.mdb)};" & _ "Dbq=" & Server.MapPath(".") & "\myDb.mdb;"

Complete Connection, SQL Statement

Create a connection:
Set connection = Server.CreateObject("ADODB.Connection")
Define the SQL statement:
sSQL = "INSERT into comments (vname, email, comments) values ('" & _
vname & "', '" & email & "', '" & comment & "')“
Open the connection & Execute:
connection.Open(sConnString)
connection.execute(sSQL)

Close the Connection

Last but not least it is very important to close the database connection:
<%
connection.Close
Set connection = Nothing
%>

Resources

For more information on javascript Validation take a look at:
http://www.javascript-coder.com/html-form/javascript-form-validation.phtml
Server Side Validation with ASP:
http://www.4guysfromrolla.com/webtech/020799-1.shtml
Database connection strings:
http://www.able-consulting.com/MDAC/ADO/Connection/ODBC_DSNLess.htm

About Us | Site Map | Advanced Search | Privacy Policy | Contact Us | ©1999-2006 HAL-PC Web Technologies SIG Leader Cheryl D. Wise