Home arrow ASP arrow Page 2 - Be My Guest in ASP
ASP

Be My Guest in ASP


Creating a guest book in ASP has never been so simple thanks to Luca. Luca's code along with a MS Access Database, may successfully generate a consistent guest book which can store a fixed amount of entries per page.

Author Info:
By: Luca Ruggiero
Rating: 3 stars3 stars3 stars3 stars3 stars / 116
July 24, 2003
TABLE OF CONTENTS:
  1. · Be My Guest in ASP
  2. · The Article
  3. · Conclusion

print this article
SEARCH DEVARTICLES

Be My Guest in ASP - The Article
(Page 2 of 3 )

What We Need In Order To Produce A Guest book

Now we analyse the files that is needed to create our Guest book:

- index.asp

It contains the input module and it prints the messages in output.

- insert.asp

As previously stated, it verifies that data are corrected, then it proceeds to their recording.

- guestbook.mdb

It is the MS Access database in which stability recording messages on the web server.

Database Structure

The DBMS we are using for this application is Ms Access 2000.
Create the folder "guestbook" and save here the new the database file, then create a new table with following fields:

-id (counter :: Primary Key)
-date (text :: we don't need a field "date", it's just a string!)
-nickname (text)
-email (text :: allow null)
-homepage (text :: allow null)
-subject (text)
-city (text)
-message (memo)

Save the table like "messages" and close Access, the database is ready to work!

Messages Recording And Writing

Here comes the time to create the file index.asp, which contains the module and the message list and through a QueryString, will show only 10 messages per page.

Of continuation I expose the commented code. Later on we will summarise the crucial points:

<%@LANGUAGE = JScript%>
<%

// I CREATE A CONNECTION WITH THE DATABASE
var Cn = new ActiveXObject("ADODB.Connection");
var Sc = "driver={Microsoft Access Driver (*.mdb)};dbq=" + Server.MapPath("guestbook.mdb");
Cn.Open(Sc);

// I RECOVER DATA FROM THE MODULE
var Sql = "SELECT * FROM message ORDER BY id DESC";

// I USE THIS CODE TO SHOW 10 MESSAGES 'PER PAGE'
var Display = new ActiveXObject("ADODB.Recordset");
Display.Open(Sql,Cn,1);
var page = parseInt(Request.QueryString("id"));
if (isNaN(page) || page < 1) page = 1;
if (!Display.EOF) {

By using the Display object we are able to set how many entries that we wish to display per page. In this case, the number of entries to be displayed is 10.

// I WANT TO READ 10 MESSAGE PER PAGE - HERE'S THE SETTING NUMBER
Display.PageSize = 10; 
Display.AbsolutePage = page;
}
var i = 0;
%>
<html>
<head>
<title>lukeonweb.net Guestbook - www.lukeonweb.net</title>
<style type="text/css">
td, div { font-size: 12px; font-family: Verdana; }
a { color: #FFFFFF; text-decoration: None; }
a:hover { color: #FFFFFF; text-decoration: None; }
a.x { color: #3366CC; text-decoration: None; }
a:hover.x { color: #FF0000; text-decoration: Underline; }
</style>
</head>
<body text="#192939">
<div align="center"><b>Comfirm My Guestbook</b></div>
<br>
<div align="center" style="color: #FF0000;"><b>*</b> Required fields</div>
<br>

To add an entry into a guest book, we should use a HTML form with a number of text boxes to be inputted by the user for his or her details. This data will be sent to insert.asp, which will be discussed later.

<!-- THIS IS THE INPUT MODULE -->
<table align="center" cellpadding="1" cellspacing="0">
<form method="post" action="insert.asp">
<tr>
<td><b>Nickname *</b></td>
<td align="right"><input type="text" name="nickname"></td>
</tr>
<tr>
<td><b>Email</b></td>
<td align="right"><input type="text" name="email"></td>
</tr>
<tr>
<td><b>Homepage</b></td>
<td align="right"><input type="text" name="homepage"></td>
</tr>
<tr>
<td><b>City *</b></td>
<td align="right"><input type="text" name="city"></td>
</tr>
<tr>
<td><b>Subject *</b></td>
<td align="right"><input type="text" name="subject"></td>
</tr>
<tr>
<td colspan="2"><b>Message *</b></td>
</tr>
<tr>
<td colspan="2"><textarea name="message" rows="5" cols="30"></textarea></td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="submit" value=" Comfirm ">
<input type="reset" value="Cancel">
</td>
</tr>
</form>
</table>
<br>

<!-- THIS IS THE ASP CODE THAT SHOWS THE MESSAGES -->
<%while((!Display.EOF) && (i<10)){%>
<table align="center" width="700" cellpadding="1" cellspacing="0" style="border: Solid 1px #192939;">
<tr bgcolor="#192939">
<td width="500" style="color: #FFFFFF;">
<b>Message <%=Display("nickname")%> (da <%=Display("city")%>)</b>
</td>
<td width="200" align="right">
<%if((Display("email") == "") || (Display("email") == "undefined")){%>
 
<%}%>
<%else {%>
<a href="mailto:<%=Display("email")%>">[email]</a>
<%}%>
<%if((Display("homepage") == "") || (Display("homepage") == "undefined")){%>
 
<%}%>
<%else {%>
<a href="<%=Display("homepage")%>" target="_blank">[homepage]</a>
<%}%>
</td>
</tr>
<tr>
<td colspan="2">
<b><%=Display("subject")%></b><br><br>
<%=Display("message")%><br><br>
<div align="right" style="font-size: 10px;">
Published in date <%=Display("date")%>
</div>
</td>
</tr>
</table>
<br>
<%
i++;
Display.MoveNext();
}
%>

<!-- THIS CODE IS NEEDFUL TO NAVIGATE AND READ THE OLD MESSAGES -->
<table align="center" width="700" cellpadding="0" cellspacing="0">
<tr>
<td width="50%">
<%if (page > 1) {%>« <a href="index.asp?id=<%=page - 1%>" class="x">Previous</a><%}%>
<%else {%><font color="#778899">« Previous</font><%}%>
</td>
<td width="50%" align="right">
<%if (!Display.EOF) {%><a href="index.asp?id=<%=page + 1%>" class="x">Following</a> »<%}%>
<%else {%><font color="#778899">Following »</font><%}%>
</td>
</tr>
</table>
</body>
</html>
<%Cn.Close()%>

The style of the application is not important in this moment. You can create a personal style that looks fine for you!

We have to concentrate on the code commented with the sentence THIS IS THE ASP CODE THAT SHOWS THE MESSAGES.

As you may have noticed, I used a while loop to read all the records till the End Of the File, and had to specify once again the number of the records I want to read in one page.

Once this is completed, I needed to use my recordset variable Display to extract single fields in the correct position on the page.

For example, Display("email") prints out the users email address closing the connection on completion.

MESSAGE RECORDING

Next is the final (and most important) step. We are going to create the file named insert.asp which will provide to the message recording on the database.

This is the commented code:

Firstly, we will use the Request object to collect the data submitted from the form.

<%@LANGUAGE = JScript%>
<%
// I RECOVER THE FIELDS FROM THE MODULE
var nickname = new String(Request.Form("nickname"));
var email = new String(Request.Form("email"));
var homepage = new String(Request.Form("homepage"));
var city = new String(Request.Form("city"));
var subject = new String(Request.Form("subject"));
var message = new String(Request.Form("message"));

Using the Data() object to get today's date, we are able to manipulate it by calling it's relative functions.

// I WRITE THE DATE
var today = new Date();
var date = today.getDate() + "/"
+ (today.getMonth() + 1) + "/"
+ today.getYear() + " Time is "
+ today.getHours() + ":"
+ today.getMinutes() + ":"
+ today.getSeconds();

Let us preform some error checking so we collect valid entries for the user's nickname, city, subject and message. If any of the entries is invalid, the response will be to redirect the page back to index.asp so the user can re-enter the data back into the form.

// I VERIFY THAT REQUIRED FIELD HAS BEEN COMPILED
if ((nickname == "") || (nickname == "undefined")) Response.Redirect("index.asp");
if ((city == "") || (city == "undefined")) Response.Redirect("index.asp");
if ((subject == "") || (subject == "undefined")) Response.Redirect("index.asp");
if ((message == "") || (message == "undefined")) Response.Redirect("index.asp");
//I CREATE CONNECTION AND RECORDSET OBJECTS
var Cn = new ActiveXObject("ADODB.Connection");
var Rs = new ActiveXObject("ADODB.Recordset");

Now, it is time to connect to the database and store the entries into a new record.

// I CREATE THE CONNECTION STRING
var Str = "driver={Microsoft Access Driver (*.mdb)};dbq=" + Server.MapPath("guestbook.mdb");

// I OPEN THE CONNECTION
Cn.Open(Str);

// I OPEN THE RECORDSET OBJECT TO UPDATE THE DB WITH THE NEW MESSAGE
Rs.Open("message",Cn,3,3);
Rs.AddNew();
Rs("nickname") = nickname;
Rs("email") = email;
Rs("homepage") = homepage;
Rs("city") = city;
Rs("subject") = subject;
Rs("message") = message;
Rs("date") = date;
Rs.Update();
Rs.Close();
// I CLOSE THE CONNECTION
Cn.Close();
// REDIRECT TO THE GUEST BOOK HOMEPAGE
Response.Redirect("index.asp");
%>

The concept is easy. I recover date from the module and I write the date using JScript Date() function, I check out the required fields. Note that the Email field and Homepage fields are optional, so the NULL value will be passed into those fields if they are left blank.

Now, you have seen that I have opened a connection to the MS Access database and recorded the data that was submitted via a form. I used a ADODB recordset object, Rs to communicate with the database. Once I finished with database, I closed the connection and I redirected the user to the Guest book homepage, so he can read his new message! There you have it, a usable Guest book to implement in your own web site.


blog comments powered by Disqus
ASP ARTICLES

- Central Scoreboard with Flash and ASP
- Calorie Counter Using WAP and ASP
- Creating PGP-Encrypted E-Mails Using ASP
- Be My Guest in ASP
- Session Replacement in ASP
- Securing ASP Data Access Credentials Using t...
- The Not So Ordinary Address Book
- Adding and Displaying Data Easily via ASP an...
- Sending Email From a Form in ASP
- Adding Member Services in ASP
- Removing Unconfirmed Members
- Trapping HTTP 500.100 - Internal Server Error
- So Many Rows, So Little Time! - Case Study
- XDO: An XML Engine Class for Classic ASP
- Credit Card Fraud Prevention Using ASP and C...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials