CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Can't get PERL to process HTML form

 



02walshe
Novice

Feb 27, 2011, 9:09 AM

Post #1 of 12 (3298 views)
Can't get PERL to process HTML form Can't Post

Hi,

I'm a n00b to this, and I'm having real problems getting this script to process my HTML form data. Essentially I want to write my data to a file on the server for later use/download.
This is what I've come up with already, but it just seems to error out every way I try:


Code
#!/usr/bin/perl 
use CGI;
use CGI::Carp qw(fatalsToBrowser);

open(BOOKINGFILE, ">>bookingform.csv"); # Open for appending

my $first_name=param{'firstname'};
my $last_name=param{'lastname'};
my $email_address=param{'emailaddress'};
my $email_address_confirmation=param{'emailaddressconfirmation'};

print BOOKINGFILE "$last_name|$first_name|$email_address|$email_address_confirmation\n";
close (BOOKINGFILE);


Any suggestions where I'm going wrong?

Many Thanks,

02walshe


Zhris
Enthusiast

Feb 27, 2011, 9:23 AM

Post #2 of 12 (3294 views)
Re: [02walshe] Can't get PERL to process HTML form [In reply to] Can't Post

Hello,

Could you be more explicit than "error out", what is the error?

I haven't tested but you are accessing the CGI module non OO, therefore I have a feeling you need to tell the module that. You'll also want to print a content type:

Try replacing:

Code
use CGI; 
use CGI ':standard';
print "Content-type:text/html\n\n";



(This post was edited by Zhris on Feb 27, 2011, 9:28 AM)


FishMonger
Veteran / Moderator

Feb 27, 2011, 9:26 AM

Post #3 of 12 (3293 views)
Re: [02walshe] Can't get PERL to process HTML form [In reply to] Can't Post

param is a function/subroutine not a hash, so you need to use ( ) parens instead of { } braces.

Every Perl script you write should include the strict and warnings pragmas. They will help you by pointing out lots of coding mistakes like you have in this script.

It's better to use a lexical var for the filehandle instead of the bareword and you should be using the 3 arg form of open and you should ALWAYS check the return code of an open call to make sure it was successful and take action if it wasn't.

When closing a write/append filehandle, it's best practice to check its return code to make sure it was successful.

For readability, it's best practice to add a little horizontal whitespace on those assignments.


Code
#!/usr/bin/perl 

use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser);

open(my $booking_fh, '>>', "bookingform.csv")
or die "failed to open 'bookingform.csv' $!";

my $first_name = param('firstname');
my $last_name = param('lastname');
my $email_address = param('emailaddress');
my $email_address_confirmation = param('emailaddressconfirmation');

print $booking_fh "$last_name|$first_name|$email_address|$email_address_confirmation\n";
close $booking_fh or die "failed to close \$booking_fh $!";



(This post was edited by FishMonger on Feb 27, 2011, 9:29 AM)


02walshe
Novice

Feb 27, 2011, 9:33 AM

Post #4 of 12 (3285 views)
Re: [Zhris] Can't get PERL to process HTML form [In reply to] Can't Post

Hi,

Sorry; forgot about that Blush

the EM I get is:


Quote
Can't locate object method "param" via package "firstname" (perhaps you forgot to load "firstname"?) at form.cgi line 10.


I'll have a go with the CGI standard command and the print content type as well.

Many Thanks,

02walshe


Zhris
Enthusiast

Feb 27, 2011, 9:36 AM

Post #5 of 12 (3283 views)
Re: [02walshe] Can't get PERL to process HTML form [In reply to] Can't Post

Ok, I think that could well be your solution, but you must consider all of FishMonger's points in order to improve your script further.

Chris


02walshe
Novice

Feb 27, 2011, 9:39 AM

Post #6 of 12 (3282 views)
Re: [FishMonger] Can't get PERL to process HTML form [In reply to] Can't Post

Hi,

OK, I'm gonna have to go back to the books on the hash/function difference.

The strict and warnings, would these still display on a web browser? (I don't have access to my server's command line unfortunately)

I'll give the below a try...

Many Thanks,

02walshe


Zhris
Enthusiast

Feb 27, 2011, 9:45 AM

Post #7 of 12 (3278 views)
Re: [02walshe] Can't get PERL to process HTML form [In reply to] Can't Post

There is usually very little difference between a Perl script you would run in the browser and one you would run via command line. The usual differences are in the initial modules used and explicitly printing the content type if required.

Heres how I would usually start a Perl CGI script, set to display fatal errors and warnings in the browser:


Code
#!/usr/bin/perl 
use strict;
use CGI ':standard';
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
print "Content-type: text/html\n\n";
warningsToBrowser(1);


And same again for a command line run code:


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



02walshe
Novice

Feb 27, 2011, 10:05 AM

Post #8 of 12 (3272 views)
Re: [Zhris] Can't get PERL to process HTML form [In reply to] Can't Post

Hi,

I'll keep that in mind for my next script; I might start out with something a little easier! Smile

Thanks for you advice on this one, the script seems to be working now.

Many Thanks!

02walshe


FishMonger
Veteran / Moderator

Feb 27, 2011, 10:13 AM

Post #9 of 12 (3271 views)
Re: [Zhris] Can't get PERL to process HTML form [In reply to] Can't Post

This is just a matter of personal preference, but I prefer to use CGI's OO interface instead of the functional interface. I also prefer to use a little more whitespace, so here's how I usually start cgi scripts.


Code
#!/usr/bin/perl 

use strict;
use warnings FATAL => 'all';
use CGI;
use CGI::Carp qw( fatalsToBrowser ); # remove after developing/debugging

my $cgi = CGI->new;
my %form = $cgi->Vars; # if I'm processing a form submission

print $cgi->header, $cgi->start_html;



Zhris
Enthusiast

Feb 27, 2011, 10:50 AM

Post #10 of 12 (3269 views)
Re: [FishMonger] Can't get PERL to process HTML form [In reply to] Can't Post

I've really got to start using the full OO interface of CGI. It has become a habit to use the functional interface since this is the way I first learnt. How would warnings be handled in your example, are they printed to the browser as html comments?


Zhris
Enthusiast

Feb 27, 2011, 10:55 AM

Post #11 of 12 (3267 views)
Re: [02walshe] Can't get PERL to process HTML form [In reply to] Can't Post

I just had to mention 1 more thing because its extremely important:

ALWAYS VALIDATE FORM INPUT.

You never know what kind of data could be passed and the implementations it may have.

Goodluck,

Chris


FishMonger
Veteran / Moderator

Feb 27, 2011, 1:47 PM

Post #12 of 12 (3257 views)
Re: [Zhris] Can't get PERL to process HTML form [In reply to] Can't Post

In the example you gave earlier the warnings would show up as html comments. The example I gave raises the warnings to be fatal errors and would show up in the rendered page along with all other fatal errors.

Handling fatal errors in this manor is not very eloquent, so I often write my own $SIG{__DIE__} handler. However, there are cpan modules that I have never used which are designed for this purpose and would be better than writing my own handler.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives