SiteExperts.com Logo Home | Community | Developer's Paradise | Jobs
User Groups | Site Tools | Site Information | Search

Inside Technique : Adding Discussion Forums : User Pages - Viewing Messages

Now we take you through the two asp pages for displaying messages and starting new topics.

viewTopic.asp

When you view an individual topic, we first display the original message then we display all the replies. For this page, we did not implement chunking of the replies. In an active message board you may want to enhance this page with message chunking. You can easily add chunking by copying the script from the previous page.

<%@ Language=VBScript %>
<% option explicit 

dim topicid

' The topic being viewed
topicid = Request.QueryString("topic_id")

' Make sure topic is a number
if topicID="" or not isnumeric(topicID) then Response.redirect("forums.asp")

Dim oCmd, oRS

if Request.Form("action")=actionSave then
  ' Save a message reply
  ' Replies are submitted directly back to the viewing page.
  ' This reply is automatically returned with the query that populates the page.
  Set oCmd = GetStoredProcedure(getConnection(),"sp_StartTopic")
  oCmd.Parameters.append oCmd.CreateParameter("forum_id", adInteger, adParamInput,10,Request.Form("forum_id"))
  oCmd.Parameters.append oCmd.CreateParameter("topic_parent", adInteger, adParamInput,10,Request.Form("topic_id"))
  oCmd.Parameters.append oCmd.CreateParameter("u_id", adInteger, adParamInput,10,session("u_id"))
  oCmd.Parameters.append oCmd.CreateParameter("topic_name", adVarChar, adParamInput,200,"")
  oCmd.Parameters.append oCmd.CreateParameter("topic_message", adLongVarChar, adParamInput,32000,escapeStringWithCR(Request.Form("topic_message")))
  oCmd.execute()
  Response.Write("Message Posted")
end if

' Get Message for specified topic
Set oCmd = GetStoredProcedure(getConnection(),"sp_getMessagesForTopic")
oCmd.Parameters.append oCmd.CreateParameter("topic_id", adInteger, adParamInput,10,topicID)
set oRS = oCmd.execute()

' Two recordsets are returned. The first recordset
' contains forum information, the number of replies, 
' and the original message

' If bad forum ID no records are returned
if oRS.eof then Response.Redirect("forums.asp")

' Get forum information and reply count
Dim sForumName, sForumDesc, iForumID, iReplies
iForumID = oRS.fields("forum_id")
sForumName = oRS.fields("forum_name")
iReplies = oRS.fields("topic_count")

%>
<HTML>
<HEAD>
<!-- #include virtual="/inc/utility.asp" -->
<TITLE><%=sForumName%></TITLE>
</HEAD>
<BODY>

<%

const actionSave = "Post..."

Response.Write("<H2>" & HOMEPAGE & " : <A HREF=""forums.asp"">Discussion Forums</A> : <A HREF=""viewforum.asp?forum_id=" & iforumid & """>" & sForumName & " Forum</A></H2><P>")

' Output original message
Response.Write("<TABLE BGCOLOR=lightgrey WIDTH=400><TR><TD><FONT SIZE=+1>"
Response.Write("<B>" & oRS.fields("topic_name") & "</B></FONT></TD></TR>")
Response.Write("<TR><TD>" & oRS.fields("topic_message") & "</TD></TR>")
Response.Write("<TR><TD><SMALL><EM>Started By " & oRS.fields("u_name"))
Response.Write(" on:" & oRS.fields("topic_create") & "</EM></SMALL></TD></TR></TABLE>")

' Get next recordset. This contains all the replies
' to the original message
set oRS = oRS.nextrecordset

' If replies, start outputting
if (not oRS.eof) then
  ' First display number of replies
  if iReplies=1 then
    Response.Write("<P>1 Reply")
  else
    Response.Write("<P>" & iReplies & " Replies")
  end if
  ' Output each reply
  while not oRS.eof
    Response.Write("<P><B>" & oRS.fields("u_name") & " on " & oRS.fields("topic_update") & "</B><BR>")
    Response.Write(oRS.fields("topic_message"))
    oRS.movenext
  wend
end if

' If user logged in display "reply" form
if session("u_id")<>"" then
  Response.Write(BuildForm("startTopic","post"))
  ' Store topic and forum in a hidden field
  Response.Write(BuildInput("hidden","topic_id",TopicID,10,10))
  Response.Write(BuildInput("hidden","forum_id",iForumID,10,10))
  Response.Write("<HR ALIGN=LEFT WIDTH=400><B>Post Reply...</B></BR>")
  Response.Write("<TABLE>")
  Response.Write("<TR><TD>User Name:</TD><TD>" & Session("u_name") & "</TD></TR>")
  Response.Write("<TR><TD VALIGN=TOP>Message:</TD><TD>" & BuildTextArea("topic_message","",10,35) & "</TD></TR>")
  Response.Write("<TR><TD COLSPAN=2>" & BuildInput("submit","action",ACTIONSAVE,"","") & "</TD></TR>")
  Response.Write("</TABLE>")		
else
  ' Not logged in - can't reply
  Response.Write("<P>You must <A HREF=""/members/logon.asp"">Log-on</A> to post replies")
end if	
%>
</BODY>
</HTML>

The last task for creating the user pages is to create a page for starting new topics.

startTopic.asp

Compared to the previous page, this page is quite simple. It contains a form for starting new topics. For simplicity, we filter and automatically escape all HTML. This can be easily changed by modifying the escape* functions contained within the utility.asp file. Allowing HTML can create a real challenge to administrating and managing your discussion forums. We allow HTML on our message boards. However, our algorithm for checking HTML is very simplistic so we often have to manually fix messages that unintentionally disrupt the page's layout.

<%@ Language=VBScript %>
<% option explicit 


dim forumID

' Start topic in which forum
forumID = Request.QueryString("forum_id")
if forumID="" or not isnumeric(forumid) then Response.redirect("forums.asp")

' Make sure logged in
if session("u_id")="" then Response.Redirect("/members/logon.asp")

Dim oCmd, oRS

' Get forum information
Set oCmd = GetStoredProcedure(getConnection(),"sp_getForum")
oCmd.Parameters.append oCmd.CreateParameter("forum_id", adInteger, adParamInput,10,forumID)
set oRS = oCmd.execute()

' Make sure valid forum
if oRS.eof then Response.Redirect("forums.asp")

Dim sForumName, sForumDesc
' Retrieve forum information
sForumName = oRS.fields("forum_name")
sForumDesc = oRS.fields("forum_desc")

oRS.close()

%>
<HTML>
<HEAD>
<!-- #include virtual="/inc/utility.asp" -->
<TITLE><%=sForumName%></TITLE>
</HEAD>
<BODY>

<%

const actionSave = "Post..."

Response.Write("<H2>" & HOMEPAGE & " : <A HREF=""forums.asp"">Discussion Forums</A> : <A HREF=""viewforum.asp?forum_id=" & forumid & """>" & sForumName & " Forum</A></H2><P>")

if Request.Form("action")=actionSave then
  ' Save new topic
  Set oCmd = GetStoredProcedure(getConnection(),"sp_StartTopic")
  oCmd.Parameters.append oCmd.CreateParameter("forum_id", adInteger, adParamInput,10,forumID)
  oCmd.Parameters.append oCmd.CreateParameter("topic_parent", adInteger, adParamInput,10,-1)
  oCmd.Parameters.append oCmd.CreateParameter("u_id", adInteger, adParamInput,10,session("u_id"))
  ' Escape all HTML
  oCmd.Parameters.append oCmd.CreateParameter("topic_name", adVarChar, adParamInput,200,escapeString(Request.Form("topic_name")))
  oCmd.Parameters.append oCmd.CreateParameter("topic_message", adLongVarChar, adParamInput,32000,escapeStringWithCR(Request.Form("topic_message")))
  oCmd.execute()
  Response.Write("<B>Topic: " & escapeString(Request.Form("topic_name")) & "</B><BR>")
  Response.Write(escapeStringwithCR(Request.Form("topic_message")))
  Response.Write("<P><A HREF=""viewForum.asp?forum_id=" & forumID & """>View Messages...</A>")
else
  ' Output form for posting message
  Response.Write("<P>Start new topic in " & sForumName)
  Response.Write(BuildForm("startTopic","post"))
  Response.Write("<TABLE>")
  Response.Write("<TR><TD>Forum:</TD><TD>" & sForumName & "</TD></TR>")
  Response.Write("<TR><TD>User Name:</TD><TD>" & Session("u_name") & "</TD></TR>")
  Response.Write("<TR><TD>Topic:</TD><TD>" & BuildInput("text","topic_name","",20,200) & "</TD></TR>")
  Response.Write("<TR><TD valign=top>Message</TD><TD>" & BuildTextArea("topic_message","",10,35) & "</TD></TR>")
  Response.Write("<TR><TD COLSPAN=2>" & BuildInput("submit","action",ACTIONSAVE,"","") & "</TD></TR>")
  Response.Write("</TABLE>")			
end if

%>
</BODY>
</HTML>

This defines all the user pages for the discussion forum. Next we take you through the pages for administrating and managing the message boards.