ASP Advanced Tutorial Ⅱ: A simple guest book

Source: Internet
Author: User
Tags include readline



Starting from this chapter we will enter our tutorials by a simple, guest book program that uses file storage to access the message. To write such a guest book, we can roughly take the following three steps to complete.

Step one: First design a message form page (such as: guestbook1.asp, this does not need me to preach here? )。 See figure below:


Step two: After the design of the message form, we began to write processing the message of the ASP program.

We want to write the processing of the ASP program message, the most important question is how to implement access to the message. In the ASP program, to access the file, we must establish the FileSystemObject object (which is used primarily for reading Unicode or ASCII files). To create a FileSystemObject object, we can use the following statement:
SET Object instance = CreateObject ("Scripting. FileSystemObject ")
Methods for FileSystemObject objects include: CreateTextFile and OpenTextFile:
CreateTextFile: Create a file name and return a TextStream object to provide you with read and write files
OpenTextFile: Open an existing file and create a TextStream object for you to read and write files after you create and open an editable file, we can finally invoke the method WriteLine under the TextStream object ( Writes information to an open file and wraps it, and method ReadLine (reads one line of information for an open file) to access the file.
Therefore, to achieve access to the message of the ASP program, we can be roughly conceived as follows:
Create a FileSystemObject object first, and then invoke OpenTextFile to open the file or call CreateTextFile to make a new file, and to establish a TextStream object Finally, we make a call to the TextStream object's WriteLine and ReadLine to access the file. After solving this problem, we can write the ASP program that handles the message as follows:
<%
Name = Request ("Name")
email = Request ("email")
Subject = Request ("Subject")
Memo = Request ("Memo")
Get data from a message form
If Name = "" or Email = "" or Subject = "" or Memo = "" Then
Response.Write "Input box cannot be blank!"
Response.End
End If
Check the form's input boxes for information input, if left blank input box does not fill out the display information: "The input box can not be blank!" and stop executing the following program.
Line1 = "Message person:" & Name
Line1 = Line1 & String (5, "")
email = "< A href=mailto:" & email & ">" & Email & "</a>"
Line1 = Line1 & "Email? "& Email & < Br>"
Line2 = "Subject:" & Subject & "< br>"
Memo = Replace (Memo, VbCrLf, "< br>")
Line3 = "< TABLE border=1>< tr>< td>"
Line3 = Line3 & "Your message:" & Memo & "</td></tr></table>"
Line4 = "Time:" & Now ()
Define the display format of the message, in which message information box up.
On Error Resume Next
If you do not read the following ReadAll (read all the information for an open file), the error of the program caused by the unreadable information will occur, so add this sentence to ignore all errors.
Set FSO = Server.CreateObject ("Scripting.FileSystemObject") Application.Lock
Txtpath = Server.MapPath ("Book1.txt")
NewPath = Server.MapPath ("Newbook.txt")
Fso. MoveFile Txtpath, NewPath
Set fin = fso. OpenTextFile (NewPath,, True)
Set fout = fso. CreateTextFile (Txtpath)
Create a FileSystemObject object and lock the Application object so that no two Internet users can perform the message at the same time, and create and open a TextStream object that points to the text book1.txt to be operated on for the message record.
Fout. WriteLine Line1
Fout. WriteLine Line2
Fout. WriteLine Line3
Fout. WriteLine Line4
Fout. WriteLine "< hr>"
Fout. WriteLine Fin. ReadAll
Write the information branch in the message form to the open file.
Fin. Close
Fso. DeleteFile NewPath, True
Application.UnLock
Response.Redirect "Guestbook1.asp"
Close and remove the gbookold.txt, then unlock the Application object, and then direct the client browser to the new Web page guestbook1.asp.
%>
Copy the above code to your Notepad note: Remember to remove the extra space from the "<" symbol and save it as a file manage1.asp.

Step Three: Design the message page

Because we want the message from this book to appear under the message form, we can copy the following HTML code to the end of guestbook1.asp.
< html>
< head>
< meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
< title> your message </title>
< link rel= "stylesheet" href= ". /images/text.css ">
< body text= "#000000" bgcolor= "#FFFFFF" background= "b01.jpg" >
< h2 align= "CENTER" > Your message < hr noshade color= "Red" >
<!--#include file= "Book1.txt"-->
</body>
<% Response.End%>

After completing all of the steps above, save guestbook1.asp and manage1.asp to the Web server's executable virtual directory (such as: asp/) and browse in the browser using HTTP, such as: http://yourcomputername/ Asp/guestbook1.asp. Look! As simple as that, you already have your own guest book. Don't try it quickly!



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.