add text to page from form

caniwi
caniwi used Ask the Experts™
on
I have put together some code from a few sources and don't know if it will work. I have to pay someone to set the permissions on the server and don't want to ftp it unless there is a good chance it will work. This is similar to a guestbook but it is to add storys about the kids.


#!/usr/local/bin/perl
use CGI;
$anecdotes = '/full/path/to/anecdote/file/anecdote.htm';
$anecdoteurl= 'http://www.mypage.com/anecdote.htm';

# Get the form variables
&ReadParse();
$date = $in{'date'};
$anecdote = $in{'anecdote'};

# Read content
open(ANECDOTE,"$anecdotes");
@arrancedotes = <ANECDOTE>;
close(ANECDOTE);


#Save back new content
open(ANECDOTE,">$anecdotes");
foreach $line (@arranecdotes)
{
     print ANECDOTE "$line";
     if ($line =~ '--addhere--')
     {
          print ANECDOTE "<p>$date</p>\n";
          print ANECDOTE "<p>$anecdote</p><hr>\n";
     }
}
close(ANECDOTE);

print "Content-type: text/html\n";
$anec = "$anecdoteurl\n\n";
print qq(Location: $anec);
return;

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2007

Commented:
There's a number of problems.  Which I'll go through step by step.

#!/usr/local/bin/perl

When developing code, it helps to have warnings on.  In Perl versions older than 5.6.0, use

#!/usr/local/bin/perl -w

In modern versions of Perl, put a

use warnings;

near the top of the script.

Also highly recommended is:

use strict;

This forces you to declare variables and saves you considerable hair pulling when you are debugging larger scripts.  Using strict mode would have found that you misspelt @arrancedotes as @arranecdotes




use CGI;

Good to see you use the standard CGI module.  You should also include:

use CGI::Carp qw(carpout fatalsToBrowser);

to enable you to see any errors in the browser rather than getting a "500 internal server error" and hunting around in the webserver error logs.

# Get the form variables
&ReadParse();

The ReadParse routine is mostly for cgi-lib compatability of old scripts.  Best off to use the more standard functions of the CGI module.

open(ANECDOTE,"$anecdotes");

You shoudl *always* check the success of opening a file.  So many people have problems which would have been fixed straight away if they used error checking.

open ANECDOTE, $anecdotes or die "Can not open $anecdotes $!\n";

The $! variable contains the actual reason from the system why it failed, eg: incorrect permissions, incorrect path etc.

if ($line =~ '--addhere--')

The =~ is used to check against a pattern.  You should have used eq instead.

print "Content-type: text/html\n";

You're doing a redirect, so printing a text/html header will cause the redirect to be ineffective.


return;

You're not returning to anywhere, so don't include it.

To put it all together, the script will look like this:


#!/usr/local/bin/perl
use strict;
use warnings;

use CGI;
use CGI::Carp qw(carpout fatalsToBrowser);

my $anecdotes = '/full/path/to/anecdote/file/anecdote.htm';
my $anecdoteurl= 'http://www.mypage.com/anecdote.htm';

# Get the form variables
my $q = new CGI;
my $date = $q->param('date');
my $anecdote = $q->param('anecdote');

# Read content
open ANECDOTE,$anecdotes or die "Can not open $anecdotes $!\n";
my @arrancedotes = <ANECDOTE>;
close ANECDOTE;

#Save back new content
open ANECDOTE,">$anecdotes" or die "Can not open $anecdotes for writing $!\n";

foreach my $line (@arrancedotes) {
    print ANECDOTE $line;
    if ($line eq '--addhere--') {
       print ANECDOTE "<p>$date</p>\n";
       print ANECDOTE "<p>$anecdote</p><hr>\n";
    }
}

close ANECDOTE;

print $q->redirect($anecdoteurl);

Author

Commented:
Thanks this looks good I will test it. One more question. In this line:

my $anecdotes = '/full/path/to/anecdote/file/anecdote.htm';

should I have the IP?

my $anecdotes = '000.000.000.000/full/path/to/anecdote/file/anecdote.htm';

Top Expert 2007

Commented:
I would be very surprised if you had a directory named after your IP address.

Is the file on a different server to where you will be running the script?
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Author

Commented:
Hi

Sorry it's been a few days. I have set it up, but when I submit my form it redirects me to the page without writing anything to it. Here is my form:

<form action="../cgi-bin/anecdote.cgi" name=add>
               <table border=0>              
               <tr>
                    <td><b>Date</b></td><td><input type=text name=date size=50></td>
               </tr>
               <tr>
                    <td><b>Anecdote</b></td><td>
                    <textarea name=anecdote rows=6 cols=50 wrap="virtual"></textarea>
                    </td>
               </tr>
               <tr>
                    <td align=center colspan=2><br>
                    <input type=submit value="Add" onMouseOver="window.status = 'Click here to add Anecdote';return true" onMouseOut="window.status = '';">
                    <input type=reset onMouseOver="window.status = 'Click here to clear the form';return true" onMouseOut="window.status = '';">    
                    </td>
               </tr>
               </table>
               </form>



and here is the cgi:


#!/usr/bin/perl
use strict;
use warnings;

use CGI;
use CGI::Carp qw(carpout fatalsToBrowser);

my $anecdotes = '/home2/caniwi/public_html/akl/anecdote.htm';
my $anecdoteurl= 'http://www.beatt-clan.net/akl/anecdote.htm';

# Get the form variables
my $q = new CGI;
my $date = $q->param('date');
my $anecdote = $q->param('anecdote');

# Read content
open ANECDOTE,$anecdotes or die "Can not open $anecdotes $!\n";
my @arrancedotes = <ANECDOTE>;
close ANECDOTE;

#Save back new content
open ANECDOTE,">$anecdotes" or die "Can not open $anecdotes for writing $!\n";

foreach my $line (@arrancedotes) {
   print ANECDOTE $line;
   if ($line eq '--addhere--') {
      print ANECDOTE "<p>$date</p>\n";
      print ANECDOTE "<p>$anecdote</p><hr>\n";
   }
}

close ANECDOTE;

print $q->redirect($anecdoteurl);


I don't get any errors.

Top Expert 2007

Commented:
Hi caniwi.

What do you mean I submit my form it redirects me to the page without writing anything to it."

What does "it" refer to?  The redirected page?  The Anecdotes file?

Author

Commented:
Sorry

The form is one page http://www.beatt-clan.net/akl/add.htm, and the anecdotes are on another http://www.beatt-clan.net/akl/anecdote.htm'. When you submit the form you are redirected to the annecdotes page. That is all that happens. Nothing is written.

I have increased points for your trouble.
Top Expert 2007
Commented:
OK, I see what you mean now.

The reason it doesn't add anything is that is doesn't literally match the addhere comment.

Change:

if ($line eq '--addhere--') {

to

if ($line =~ /--addhere--/) {

This will ensure it matches the text '--addhere--' anywhere on the line.
The eq would work if you chomped the line
$chomp $line;
chomp takes off the \n which is a new line character.
darn fat fingers

chomp $line;
Top Expert 2007

Commented:
eq will not work, because the line it needs to match is:

      <!--addhere-->

Note whitespace and HTML comment.
Top Expert 2007

Commented:
Well, let me clarify that statement, eg will work if you have the *exact* string you want to match, but obviously in this case, that can't be guaranteed.

Author

Commented:
Thanks very much, all working now!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial