0
votes

I am trying to create a webpage on my site that takes input from a form (firstname,lastname) and process this input via a PERL CGI script and write that input to a file.

I would also like the page after script runs to display a message stating it successfully completed with links to get back to the homepage.

I am pretty sure my HTML page is correct when referencing the script so I will just post my perl script below.

Thanks in advance.

#!/usr/bin/perl
use strict; use warnings;
use CGI::Carp qw('fatalsToBrowser'); # send errors to the browser, not to the logfile
use CGI;

print header "Content-Type: text/html\n\n";

print start_html ("Receive Form Post");

my $datestring = localtime();
my $fname = param('firstname');
my $lname = param('lastname');

open (TESTFILE, '>>c:\inetpub\wwwroot\logfiles\bwrigsbee.txt') or die;

print TESTFILE "This file has been successfully edited on $datestring\n";
print TESTFILE "by $fname $lname\n";

close (TESTFILE);

print end_html;

HTML Form that is in the page that calls the script:

   <form action="logwrite.pl" method="POST">
    <table style="width:400px" border=1px border=solid border=black>
        <tr>
            <td>First name: <input type="text" name="firstname"></td>
        </tr>
        <tr>
            <td>Last name: <input type="text" name="lastname"></td>
        </tr>
        <tr>
            <td><input type="submit" value="Submit"></td>
        </tr>
    </table>
   </form>

Debug info from browser---> Undefined subroutine &main::param called at C:\studentwebusers\CIS33715\logwrite.pl line 11.

1
Post your html. You want to make sure that you are actually posting to the script in your <form action='' Also, the content-type is important. Is the request even getting to the script? Also, check your permissions - Zuzlx
I have the action correct, when I submit the form it brings me to a debug page that references errors in the script. However this is my first experience with perl and CGI and I have no idea what I am doing so I am no good at debugging. - midrigs
What errors? What do those errors say? - Zuzlx
First off, your html is not valid ` <table style="width:400px" border=1px border=solid border=black>` Your style is malformed. - Zuzlx
@Zuzlx, the HTML is indeed malformed, but this does not affect the functional issue that the question is about. - Jukka K. Korpela

1 Answers

0
votes

Remove the single quotes in your use CGI::Carp call:

use CGI::Carp qw(fatalsToBrowser);

Also, your call to header should not include the additional parameters. It could accept a type 'text/html' but all the additional info is an error

print header;

Use autodie anytime you're doing file processing. That way you won't have to be bothered to create detailed error messages. And use the 3 parameter form of open with lexical finel handles.

Finally, go ahead and use the object notation for all of methods in CGI. That will change your script to the following:

use strict;
use warnings;
use autodie;

use CGI::Carp qw(fatalsToBrowser); # send errors to the browser, not to the logfile
use CGI;

my $q = CGI->new;

print $q->header;
print $q->start_html("Receive Form Post");

my $datestring = localtime();
my $fname = $q->param('firstname');
my $lname = $q->param('lastname');

open my $fh, '>>', 'c:\inetpub\wwwroot\logfiles\bwrigsbee.txt';
print $fh "This file has been successfully edited on $datestring\n";
print $fh "by $fname $lname\n";
close $fh;

print $q->end_html;