Monday, December 17, 2007

Discussion Board - How to save the ASP form?

Discussion Board - How to save the ASP form?

We need to get the information from the form that is posted by the user. We can save it to the database:

Step 1 – How to get the information from the form?

ASP provided the Request.Form to get the content of the form.


How to do it?

<%

strUser = Request.Form("user")

strTitle = Request Form("title")

strContent = Request.Form("content")

%>


Step 2 – Create the ADO Connection and Recordset:

The following step allows you to create the ADO Connection and Recordset and use it to save the information to the database:

<%

Set conn = Server.CreateObject("Adodb.Connection")

conn.Open "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("db1.mdb")

Set rs = Server.CreateObject("Adodb.Recordset")

%>

Step 3 – Open the table and add the record:

<%

Set conn = Server.CreateObject("Adodb.Connection")

conn.Open "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("db1.mdb")

Set rs = Server.CreateObject("Adodb.Recordset")

rs.Open "Discuss", conn, 1,3

rs.AddNew

rs.Fields("User") = Request.Form("user")

rs.Fields("Title") = Request.Form("title")

rs.Fields("Content") = Request.Form("content")

rs.Fields("PostDate") = Now()

rs.Update

rs.Close

Set rs = nothing

Set conn = nothing

%>


The finally script is:

<%@LANGUAGE="VBScript"%>

<%

'First We get the value from the submit form

strUser = Request.form("user")

strTitle= Request.form("title")

strContent = Request.form("content")

%>

New Discussion

Post Your Message


<%

If strUser="" or strTitle="" or strContent="" Then

%>

User >
Title >
Message


<%

Else

Set conn = Server.CreateObject("Adodb.Connection")

conn.Open "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("db1.mdb")

Set rs = Server.CreateObject("Adodb.Recordset")

rs.Open "Discuss", conn, 1,3

rs.AddNew

rs.Fields("User") = Request.Form("user")

rs.Fields("Title") = Request.Form("title")

rs.Fields("Content") = Request.Form("content")

rs.Fields("PostDate") = Now()

rs.Update

rs.Close

Set rs = nothing

Set conn = nothing

End If

%>

No comments: