Toxiclab.org

Shoutbox tutorial

3.1.2006, 17:16
Submited in: Asp | Total Views: 51799

This tutorial will show you how to make an easyest, known as shoutbox, and how to connect to access database, see data and write data over a form.

Shoutbox works on this principle: a visitor writes a name and a message, over a form.
Then all gets proceeded on to the page where we write all of the data down in the database.
After that we return on to the page where we have form for the inscription and where we can view shoutbox messages.

The page that will carrey form for the inscription, and messages will be shown, we’ll call shoutbox.asp.
The page on which we connect to the database common.asp., and the page where we inscript data in the database upis.asp.

Let us get started in this order.

First we’ll make a database. The database will be Microsoft Access.
Open Ms access, which you have in the office package, click on the Create table in design view.
We need to make fields in the table that we will call tblshoutbox.

ID - Auto Number
Name – Text
Mmmessage – Memo

After we finish, we will save the database as db.mdb

common.asp file (connect to access database)

<%
'Dimension variables
Dim adocon
Dim strcon
Dim strSql
Dim rsShow
Dim rsAdd

'Create an ADO connection object
Set adocon = Server.CreateObject("ADODB.Connection")
strCon = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("db.mdb")
%>


shoutbox.asp

<!--#include file="common.asp" -->

<html>
<head>
<title>Shoutbox</title>
</head>

<body>

<%
Set rsShow = Server.CreateObject("ADODB.Recordset")
strSQL = "SELECT TOP 15 * FROM tblShoutbox ORDER BY ID DESC;"
rsShow.Open strSQL, strcon
%>

<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="16%">
  <tr>
<%
Do while not rsShow.EOF
%>
    <td width="100%"><b><% = rsShow("Name") %></b>-<% = rsShow("Msg") %></td>
  </tr>
<%
rsShow.MoveNext
loop
%>
</table>

<form method="POST" action="add.asp">
  Name:<br>
  <input type="text" name="name" size="20"><br>
  Msg:<br>
  <input type="text" name="msg" size="20"><br>
  <input type="submit" value="  Submit  " name="submit"></p>
</form>
</body>
</html>

<%
'Reset server objects
rsShow.close
Set rsShow = Nothing
Set adocon = Nothing
%>

And in the end add.asp, where the data received from the form will be inscribed into the database.

add.asp
<!--#include file="common.asp" -->

<%
'Create an ADO recordset object
Set rsAdd = Server.CreateObject("ADODB.Recordset")
strSQL = "SELECT tblShoutbox.* FROM tblShoutbox;"
rsAdd.CursorType = 2
rsAdd.LockType = 3
rsAdd.Open strSQL, strCon

'Add a new record
rsAdd.AddNew

rsAdd.Fields("Name")  = Request.Form("Name")
rsAdd.Fields("Msg")  = Request.Form("Msg")

rsAdd.Update
rsAdd.Requery

'Reset server objects
rsAdd.close
Set rsAdd = Nothing

'Redirect
Response.Redirect("shoutbox.asp")
%>

Let me remind you that this is the simplest shoutbox, which we used just for an example.
You can add javascript form check, option that with a help of cookie makes it possible to remember visitors name after a first written message, and more.

Are you a website owner?
Click here to register and submit your tutorials!

Browse by category


Most popular tutorials