PDA

View Full Version : Simple guest book


hkgray
10-19-2002, 09:09 AM
Dear all,

I tried to created a simplest guest book, but it was partly success only because the variable "Name" cannot transmitted to the data file. The transferred data were all shown as string "Message". There are 3 variable "Name", "Country","Message". "Country" and "Message" field is correct. But the Name field is not correct.

I guess it was an error in variable name, but I checked it and there is no error there.

Is there anyone can advise me?

Thank you.

Raymond.


===========================================
#!/usr/local/bin/perl
# register.pl - a CGI program that takes guest data from index.html and
# writes it to a text file guest.dat
# Error on open file
sub error {
print "Error $!-file could not be opened<br />\n";
print "</body>\n</html>";
exit(1);
}
# HTML Header information
print "Content-type: text/html\n\n",
"<html>\n<head>\n<title>register.pl guest",
"CGI example</title>\n</head>\n<body>\n";
# Determine the request method and get the query string
$request_method=$ENV{'REQUEST_METHOD'};
if($request_method eq "POST") { # we only want POST
read(STDIN, $query_string, $ENV{'CONTENT_LENGTH'});
}
else {
print "Error¡Vmust use POST method\n</body>\n</html>";
exit(1);
;
}
# Split the pairs into names and values and translate the values into text
@name_value_pairs=split(/&/,$query_string);
foreach $name_value(@name_value_pairs) {
($name,$value)=split(/=/,$name_value);
$name =~ tr/+/ /;
$value =~ tr/+/ /;
$value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack("C",hex($1))/eg;
chomp $value;
if($name eq "name") {
$name=$value;
}
elsif($name eq "message") {
$message=$value;
}
elsif($name eq "country") {
$country=$value;
}
}
# Write details to a text file guest.dat contained
# in the same directory as this program
$LOCK=2;
$UNLOCK=8;
open(OUTDAT,">>../guest.dat") or error();
flock(OUTDAT,$LOCK);
print OUTDAT "$name#$message#$country\n";
flock(OUTDAT,$UNLOCK);
close(OUTDAT);
# Send success message to user
print"<font face=\"Arial\" size=\"3\" color=\"red\">",
"Thank you for your details!<br /><br />\n";
print "</body>\n</html>";
==============================================
#!/usr/local/bin/perl
# guest_list.pl - a CGI program that lists the entries in guest.dat in
# alphabetic order
# Error on open file
sub error {
print "</table>";
print "Error $! - file could not be opened<br/>\n";
print "</body>\n</html>";
exit(1);
}
# HTML header information
print "Content-type:text/html\n\n",
"<html>\n<head>\n",
"<title>guest_list.pl CGI example</title>\n<\head>\n",
"<body>\n",
"<h2>List of Guests</h2>\n",
"<table border=\"1\">\n",
"<tr><th>Name</th><th>Country</th><th>Message</th></tr>";
# Attempt to open guest.dat
$LOCK=2;
$UNLOCK=8;
open(INDAT,"../guest.dat") or error();
flock(INDAT,$LOCK);
$count="0";
chomp($lines[$count]=<INDAT>);
while ($lines[$count]) {
$count++;
chomp($lines[$count]=<INDAT>);
}
@sorted_lines=sort(@lines);
$a=0;
#print rest of table to screen
while ($a<=$count) {
@values = split(/#/,$sorted_lines[$a]);
print "<tr>",
"<td>$values[0]</td>\n",
"<td>$values[1]</td>\n",
"</tr>\n";
$a++;
}
# Close files and end HTML
flock(INDAT,$UNLOCK);
close(INDAT);
print "</table><br/>\n",
"<body>\n</html>\n";
==============================================

whammy
10-19-2002, 01:53 PM
Not sure in Perl but in some languages (like javascript) "name" is a reserved word. Try something slightly different, like "guestname" or something, perhaps?