Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Your First Form using ASP Processing

Form Handling

Your First Form using ASP Processing

by  onpnt  Posted    (Edited  )
In this example we'll see how a basic Guest Book. This is a very common addition to sites to get information on site reviews, shopping experiences and in some cases just for fun.
First thing we do is creating our form. Again just some basic HTML coding.
save this page as guest.htm
<html>
<body>
<form method="post" action="guest.asp">
<table>
<tr>
<td colspan="2"><strong>Please feel free to sign my guest book and let me know how the site is.</strong></td>
</tr>
<tr>
<td><strong>Enter your info here to sign</strong></td>
<tr>
<td> Email Address:</td>
<td><input type="text" name="EmailAddr"></td>
</tr>
<tr>
<td>Name:</td>
<td><input type="text" name="Name"></td>
</tr>
<tr>
<td>Comment my site:</td>
<td><input type="text" name="comments" size="60"></td>
</tr>
<tr>
<td><input type="submit" value="Submit" name="submit2"></td>
<td><input type="reset" value="Reset" name="reset"></td>
</tr>
</table>
</form>
</body>
</html>
Next we need to write our processing script which we named guest.asp in our form,
Step one is to create your connection and our recordset. Then we execute the sql statement to populate the recordset.
<%
dim cnn,rs
set cnn = Server.CreateObject("ADODB.Connection")
set rs = Server.CreateObject("ADODB.RecordSet")
cnn.Open "driver={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("databases\guestbook.mdb")
sqltext = "SELECT * FROM tblguest"
rs.Open sqltext,cnn,3,3

Now we gather the users data from the form.
dim nm, addr, com, day
nm = Request.Form("Name")
com = Request.Form("comments")
addr = Request.Form("EmailAddr")
day = now

And lastly, we insert that data into the database
rs.AddNew
rs("Name") = nm
rs("EmailAddr") = addr
rs("Comments") = com
rs("Date") = day
rs.update
cnn.Close
Set rs=Nothing
Set cnn=Nothing

'Lets redirect the user to a page to view the results next
Response.Redirect "GuestEntries.asp"
%>

Name this page GuestEntries.asp
<%
' declare the variables
Dim Conn, dbpath

' create the connections and open them
Set Conn = Server.Createobject("ADODB.Connection")
dbpath ="DBQ=" & server.mappath("databases\guestbook.mdb")
Conn.open "DRIVER={Microsoft Access Driver (*.mdb)};" & dbpath


Dim rs, sqltxt
' create the recordset object
Set rs = Server.CreateObject("ADODB.Recordset")

'basic sql statement to select all the records fro each column in the table
sqltxt = "SELECT * FROM tblguest"

'fill the record set with the data
rs.open sqltxt, Conn

'write our headings for the table out
Response.Write "<P><TABLE>" & vbCrLf
Response.Write "<TR>" & vbCrLf
Response.Write "<TD BGCOLOR=""#F5F5F5""><B>Name</B></TD>" & vbCrLf
Response.Write "<TD BGCOLOR=""#F5F5F5""><B>Comments</B></TD>" & vbCrLf
Response.Write "<TD BGCOLOR=""#F5F5F5""><B>Email Address</B></TD>" & vbCrLf
Response.Write "<TD BGCOLOR=""#F5F5F5""><B>Date</B></TD>" & vbCrLf
Response.Write "</TR>" & vbCrLf

'loop until there are no more records to find
Do While Not rs.EOF
Response.Write "<TR>" & vbCrLf
Response.Write "<TD>" & rs("Name").Value & "</TD>" & vbCrLf
Response.Write "<TD>" & rs("comments").Value & "</TD>" & vbCrLf
Response.Write "<TD>" & rs("EmailAddr").Value & "</TD>" & vbCrLf
Response.Write "<TD>" & rs("Date").Value & "</TD>" & vbCrLf
Response.Write "</TR>" & vbCrLf
rs.MoveNext
Loop
Response.Write "</TABLE>" & vbCrLf
Conn.Close
Set rs=Nothing
Set Conn=Nothing
%>
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top