Monday, December 17, 2007

Discussion Board - How to delete the discussion?

Discussion Board - How to delete the discussion?

Step 1 – We need to list out all the discussions in a table:

<%

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

do while not rs.eof

response.write("

" & _

"

" & _

"

" & _

"

" & _

"

")

rs.movenext

loop

rs.close

set rs = nothing

set conn = nothing

%>

User Title Post Date
" & rs.fields("User") & "" & rs.fields("Title") & "" & rs.fields("PostDate") & "


Step 2 – we need to let user to select which record that they need to delete:

<%

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

do while not rs.eof

response.write("

" & _

"

" & _

"

" & _

"

" & _

"

")

rs.movenext

loop

rs.close

set rs = nothing

set conn = nothing

%>

User Title Post Date
" & rs.fields("User") & "" & rs.fields("Title") & "" & rs.fields("PostDate") & "

Step 3 – We need to use the ASP script to delete the record in database:

<%

if request.form("SubmitButton")="Delete" then ‘When the user click the Submit button

if request.form("C1").count then ‘Is the user has select any record

for i=1 to request.form("C1").count ‘If Yes, use the looping to get each ID

OrderID = request.form("C1")(i)

strSQL="DELETE FROM Discuss WHERE ID="&OrderID ‘Generate the SQL

conn.execute strSQL ‘Delete the record

next

end if

end if

%>

<%

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

do while not rs.eof

response.write("

" & _

"

" & _

"

" & _

"

" & _

"

")

rs.movenext

loop

rs.close

set rs = nothing

set conn = nothing

%>

User Title Post Date
" & rs.fields("User") & "" & rs.fields("Title") & "" & rs.fields("PostDate") & "

Copyright © 2001-02 ISC Software. All rights reserved. All other product and brand names are registered properties of their respective owners.

Discussion Board - How to show the details of the discussion?

Discussion Board - How to show the details of the discussion?

When we get the ID, How can we get the information in database?

Step 1 – We need to use Request.QueryString function to get the ID that the user selected:

intID=request.querystring("id")

Step 2 – Use the ID to generate a SQL command to get the record:

strSQL= "SELECT * FROM Discuss WHERE ID=" & intID


Step 3 – use the SQL to open a RecordSet:

<%

intID=request.querystring("id")

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

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

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

strSQL= "SELECT * FROM Discuss WHERE ID=" & intID

rs.open strSQL,conn,1,3

%>



Step 4 – Now you can get the data:

<%

intID=request.querystring("id")

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

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

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

strSQL= "SELECT * FROM Discuss WHERE ID=" & intID

rs.open strSQL,conn,1,3

If NOt rs.EOF then

strUser = rs.fields("User")

strTitle = rs.fields("Title")

strContent = rs.fields("Content")

strPostDate = rs.fields("PostDate")

end if

%>



Step 5– Display the Data:

<%

intID=request.querystring("id")

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

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

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

strSQL= "SELECT * FROM Discuss WHERE ID=" & intID

rs.open strSQL,conn,1,3

If NOt rs.EOF then

strUser = rs.fields("User")

strTitle = rs.fields("Title")

strContent = rs.fields("Content")

strPostDate = rs.fields("PostDate")

end if

%>

User <%=strUser%>
Post Date <%=strPostDate%>
Title <%=strTitle%>
Message <%=strContent%>

Discussion Board - How to create an index of the discussion?

Discussion Board - How to create an index of the discussion?

Step 1 – We need to list out all the discussions in a table:

<%

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

do while not rs.eof

response.write("

" & _

"

" & _

"

" & _

"

" & _

"

")

rs.movenext

loop

rs.close

set rs = nothing

set conn = nothing

%>

User Title Post Date
" & rs.fields("User") & "" & rs.fields("Title") & "" & rs.fields("PostDate") & "


Step 2 – We need to create a link that will show the details of the discussion.

In the HTML code, we need to create a hyperlink. So we need to use this command:

Now, the problem is, you don’t know How many links you need to create. So you need to use the ASP script!

First, we can use the Looping to create all the links:

<% For I = 1 to 10 %>

<%Next%>

But, this example only can generate 10 links to show the same file! So How can we let the user to select the different discussion titles and show the contents?

First, we understand that each discussion has its own ID, so we can use the ID to identify it.

Now, we can try to create a link like this:

‘1 is the ID of the Discuss


To create a dynamic one:


<%=rs.fields("ID")%>’> ‘We can get the ID from the database

The complete example is shown below:

<%

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

do while not rs.eof

response.write("

" & _

"

" & _

"

" & _

"

" & _

"

")

rs.movenext

loop

rs.close

set rs = nothing

set conn = nothing

%>

User Title Post Date
" & rs.fields("User") & "" & rs.fields("Title") & "" & rs.fields("PostDate") & "



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

%>

Discussion Board - How to avoid the user from sending an incomplete form?

Discussion Board - How to avoid the user from sending an incomplete form?

When we write a script, we need to make sure that the user had completed the form.

So How can we avoid the user from sending an incomplete form?


Below are the ASP scripts and descriptions:



<%@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



<%

‘Now, we check any variable is empty, if any variable is empty, which means the form is incomplete! We need to display the form again.

‘Another case is when we start a new discussion, all variable will be empty

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

%>

User >
Title >
Message



<%

Else

%>

We will save this message.

<%

End If

%>


When the user had not completed the form, the system will return a same form to the user after the user clicked on the Post button:


When the user had completed the form, this message will be displayed:


Discussion Board - How to post the first Discussion?

Discussion Board - How to post the first Discussion?

First, we need to create a form to let user post the discussion to the web server:

<%@LANGUAGE="VBScript"%>

New Discussion

Post Your Message


User
Title
Message


When you submit this form, the destination will be the same file if we set the Action parameter to the same file name:

>

Set the input type to Submit but not the Button. Only when the input type is Submit the form, it will then send the Input value to web server:

Discussion Board - How to create an Access database?

Discussion Board - How to create an Access database?

We need to create an Access database to be used in the Discussion Board:

Step 1 – Create mdb file call db1.mdb

Step 2 – Create a table in the database called Discuss

Step 3 – Insert the fields in the table as shown in the following table:

Field Name

Data Type

ID

AutoNumber

User

Text

Post Date

Date/Time

Title

Text

Content

Memo