Insert Form Data to Database

When a user submits a contact form, it is a good idea to insert a copy of that email into a database for future reference.  Here is how you can insert your form data to a simple MS Access database.

The first thing you need to do is create a form page called /Form.asp with the code below:

<form name="YourFormName" method="Post" action="confirmation.asp">
Email:  <input type="text" name="Email" size="50"><br>
Name:  <input type="text" name="Name" size="50"><br>
Comments:  <textarea name="Comments"></textarea><br>
<input type="submit" name="Submit" value="Submit Form">

Next, we create a confirmation page called /Confirmation.asp and enter the code as seen below:

' First open your database connection
<!--#INCLUDE VIRTUAL="/includes/connection.asp" -->

Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open "YOUR table name HERE", objConn, , adLockOptimistic, adCmdTable

' Add a new record to the database containing your form data
objRS("Email") = Request.Form("Email")
objRS("Name") = Request.Form("Name")
objRS("Comments") = Request.Form("Comments")

' Close your database connection
Set objRS = Nothing
Set objConn = Nothing

' You could also include an email script here to send your form via email

' Display a confirmation message
DIM strName
strName = Request.Form("Name")
Response.Write strName

<p>Thank you for emailing me.</>

Now you have a contact form that is recorded in your database that you can refer back to in the future.  This is a great way to follow up with customers and track how many people are using your website from year to year.

< Back to ASP Scripts