CGI Scripts
to Create a Website with Interactive Features
Post a Message to Board
This exercise includes an HTML form for posting a message, and a CGI script for processing the form. The HTML form should be saved in the htdocs directory. The CGI script should be saved in the cgi-bin directory.
message_brd.html
A simple HTML Form for adding a message to the board.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Message Board</title>
<meta name="DESCRIPTION" content="description of page">
<meta name="KEYWORDS" content="keywords, for, search engines">
<style type="text/css">
h2{font-family: Arial;font-style : normal ;font-size : 12pt; font-weight :bold;text-align :center }
h3{ font-family: Arial;font-style : normal ;font-size : 10pt; font-weight :bold;text-align :center}
form{ font-family: Arial;font-style : normal ;font-size : 10pt; font-weight :normal}
p{font-family: Arial;font-style : normal ;font-size : 10pt; font-weight :normal;text-align :center;padding-left : 3em;padding-right : 3em }
</style>
</head>
<body>
<div align="center">
<h2>Post New Message</h2>
<form method="GET" action="cgi-bin/post_message.pl" name="general_message">
Your Name: <input type="text" name="yourname" size="20">
Email: <input type="text" name="email" size="20"><br>
<br>
Subject: <input type="text" name="subject" size="40"><br>
<hr>
<h3>How to Post a Message</h3>
<p>Enter your message in the text box provided below. Do not use the return key. Some browsers interpret the return key as a click of the submit button. Click Submit when you are finished. If you want to receive responses by email, provide a valid email address above.</p>
<p>Enter Message</p>
<textarea name="message" cols="40" rows="8"></textarea>
<br>
<input type="submit" value="Submit"><input type="reset">
</div>
</form>
</body>
</html>
post_message.pl
This script parses the data entered on the form saves it to the message_board.txt file, reopens the file and displays the contents to the screen. Note: Some code added to parsing routine to remove illegal and unwanted characters from Query String.
#!/usr/bin/perl
#post_message.pl
$buffer=$ENV{'QUERY_STRING'};
$buffer =~ tr/+/ /;
$buffer =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
$buffer =~ s/<!--(.|\n)*-->/ /g;
$buffer =~ tr/\\|[|]|<|!|"|$|{|}|*|#|'|>|||;|%/ /;
@pairs = split(/&/,$buffer);
foreach $pair(@pairs){
($key,$value)=split(/=/,$pair);
$formdata{$key}.="$value";
}
$yourname=$formdata{'yourname'};
$email=$formdata{'email'};
$subject=$formdata{'subject'};
$message=$formdata{'message'};
open(INFO, ">>message_board.txt"); # Open for appending
print INFO "$yourname|$email|$subject|$message\n";
close (INFO);
open(INFO, "message_board.txt"); # Open db for reading and display
@array=<INFO>;
close (INFO);
print "Content-type:text/html\n\n"; #Follow with blank line
print "<html>\n";
print "<head><title>Message Board</title>\n";
print "<style type="text/css">\n";
print "h2\{font-family: Arial;font-style : normal ;font-size : 12pt; font-weight :bold;text-align :center\}\n";
print "td\{font-family: Arial;font-style : normal ;font-size : 10pt; font-weight :normal \}\n";
print "p\{ font-family: Arial;font-style : normal ;font-size : 10pt; font-weight :normal;text-align :left; width : 40em \}\n";
print "</style>\n";
print "</head>\n";
print "<body>\n";
print "<div align=\"center\">\n";
print "<h2>Message Board</h2>\n";
print "<table width=\"550\" cellpadding=\"5\" cellspacing=\"0\" border=\"1\">\n";
foreach $line (@array){
($yourname,$email,$subject,$message)=split(/\|/,$line);
print "<tr><td>\n";
print "Posted by: $yourname <a href="mailto:$email">Return Message by Email</a><br>\n";
print "Subject:$subject<br>\n";
print "<br>\n";
print "Message:<br>\n";
print "<p>$message</p>\n";
print "</td></tr>\n";
}
print "</table>\n";
print "<br><br>\n";
print "<a href=\"./message_brd.html\">Post a New Message</a>\n";
print "</div>\n";
print "</body>\n";
print "</html>\n";
Free Websites
MoonFruit
More websites
Free Website Builders
Yahoo Sitebuilder 2.6
Ucoz
Alleycode HTML
NoteTab Light
Nvu WYSIWYG
Seamonkey Composer
CSE Validator
HTML Kit
Joomla
Web Hosting Services
Web Hosting FAQ
Recommended
Web Hosts



