ren
03-09-1999, 08:00 PM
I'm very new to CGI programming. I'm trying to create a script that will open my 'news2.htm' file locate the string '<!-- News Insert -->' and replace it with '<!-- News Insert -->' followed by information from my form. I've gotten the script to the point where is outputs the information from the update form perfectly, but it doesn't amend the file the way I want it. Please please help me! Here is what I've got so far:
--------
#!/usr/bin/perl
&parse_form;
$headline = $form{'headline'};
$source = $form{'source'};
$article = $form{'article'};
$reporter = $form{'reporter'};
($min,$hour) = localtime(time);
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
if ($hour eq 00) {
$hour = "12";
$m = "am";
}elsif ($hour eq 12) {
$hour = "12";
$m = "pm";
}elsif ($hour > 12) {
$hour = $hour - 12;
$m = "pm";
} else {
$hour = "$hour";
$m ="am";
}
if ($min < 10) {
$min = "0$min";
}
if ($hour < 10) {
$hour = "0$hour";
}
$time = "$hour:$min$m";
print "Content-type: text/html\n\n";
print "<!-- News Insert -->";
print "
<UL><FONT COLOR=#FFFFFF FACE=Arial><U>$headline</U></FONT>[nbsp]<FONT FACE=Arial SIZE=1>$time EST - Source: $source</FONT>";
print "<UL><FONT FACE=Arial>$article</FONT>";
print "
<FONT FACE=Arial SIZE=1>Reported by: $reporter</FONT>[/list][/list]";
open(NEWS,"/big/dom/x3dnow/www/news2.htm")| |&ErrorMessage;
@news = <NEWS>;
close(NEWS);
open(NEWS,">>/big/dom/x3dnow/www/news2.htm")| |&ErrorMessage;
foreach $news (@news) {
if ($news =~/<!-- News Insert -->/) {
print NEWS "<!-- News Insert -->\n";
print NEWS "
<UL><FONT COLOR=#FFFFFF FACE=Arial><U>$headline</U></FONT>[nbsp]<FONT FACE=Arial SIZE=1>$time EST - Source: $source</FONT>\n";
print NEWS "<UL><FONT FACE=Arial>$article</FONT>\n";
print NEWS "
<FONT FACE=Arial SIZE=1>Reported by: $reporter</FONT>[/list][/list]\n";
}
}
close(NEWS);
sub ErrorMessage {
print "There was an error posting your news.";
}
sub parse_form {
# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
# Split the name-value pairs
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
# Un-Webify plus signs and %-encoding
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
if ($form{$name}) {
$form{$name} .= ", $value";
}else {
$form{$name} = $value
}
}
}
---------
Thanx in advance!
--------
#!/usr/bin/perl
&parse_form;
$headline = $form{'headline'};
$source = $form{'source'};
$article = $form{'article'};
$reporter = $form{'reporter'};
($min,$hour) = localtime(time);
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
if ($hour eq 00) {
$hour = "12";
$m = "am";
}elsif ($hour eq 12) {
$hour = "12";
$m = "pm";
}elsif ($hour > 12) {
$hour = $hour - 12;
$m = "pm";
} else {
$hour = "$hour";
$m ="am";
}
if ($min < 10) {
$min = "0$min";
}
if ($hour < 10) {
$hour = "0$hour";
}
$time = "$hour:$min$m";
print "Content-type: text/html\n\n";
print "<!-- News Insert -->";
print "
<UL><FONT COLOR=#FFFFFF FACE=Arial><U>$headline</U></FONT>[nbsp]<FONT FACE=Arial SIZE=1>$time EST - Source: $source</FONT>";
print "<UL><FONT FACE=Arial>$article</FONT>";
print "
<FONT FACE=Arial SIZE=1>Reported by: $reporter</FONT>[/list][/list]";
open(NEWS,"/big/dom/x3dnow/www/news2.htm")| |&ErrorMessage;
@news = <NEWS>;
close(NEWS);
open(NEWS,">>/big/dom/x3dnow/www/news2.htm")| |&ErrorMessage;
foreach $news (@news) {
if ($news =~/<!-- News Insert -->/) {
print NEWS "<!-- News Insert -->\n";
print NEWS "
<UL><FONT COLOR=#FFFFFF FACE=Arial><U>$headline</U></FONT>[nbsp]<FONT FACE=Arial SIZE=1>$time EST - Source: $source</FONT>\n";
print NEWS "<UL><FONT FACE=Arial>$article</FONT>\n";
print NEWS "
<FONT FACE=Arial SIZE=1>Reported by: $reporter</FONT>[/list][/list]\n";
}
}
close(NEWS);
sub ErrorMessage {
print "There was an error posting your news.";
}
sub parse_form {
# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
# Split the name-value pairs
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
# Un-Webify plus signs and %-encoding
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
if ($form{$name}) {
$form{$name} .= ", $value";
}else {
$form{$name} = $value
}
}
}
---------
Thanx in advance!