Wednesday, March 28, 2012

form data in Perl using CGI module (GET and POST methods)

Environment variable can be used to get GET data from forms. Using CGI module, we can get the POST data as well. A simple html form is created and this perl script is called in 'action'.

#!"D:\xampp\perl\bin\perl.exe"
use strict;
use warnings;
use CGI qw(:standard);

if ($ENV{'REQUEST_METHOD'} eq "GET")
{
    my $value = $ENV{'QUERY_STRING'};
    print "Content-type: text/html\n\n";
    my ($var1, $var2) = split(/=/,$value);  #remove the '=' symbol from the url and split the data
    print $var2;
}
elsif ($ENV{'REQUEST_METHOD'} eq "POST")
{

    my $cgi = new CGI;
    print $cgi -> header();
    print $cgi -> start_html("printing post data");
    print "Value is: \n" . $cgi -> param('value');
    print $cgi -> end_html();

}

No comments:

Post a Comment