0

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.

7
  • 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 Commented May 16, 2014 at 17:53
  • 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. Commented May 16, 2014 at 17:58
  • 1
    What errors? What do those errors say? Commented May 16, 2014 at 18:02
  • First off, your html is not valid ` <table style="width:400px" border=1px border=solid border=black>` Your style is malformed. Commented May 16, 2014 at 18:05
  • @Zuzlx, the HTML is indeed malformed, but this does not affect the functional issue that the question is about. Commented May 16, 2014 at 18:08

1 Answer 1

0

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;
Sign up to request clarification or add additional context in comments.

1 Comment

I believe your code here corrected the issue with the CGI script not working... Thank you much. Now I need the script to point back to the original HTML file with edits to the body thanking them for their input...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.