0

I would like to use an HTML form to pass a variable to Perl CGI script so that I can process that variable, and then print it out on another HTML page.

Here is my HTML code: http://jsfiddle.net/wTVQ5/.

Here is my Perl CGI script to links the HTML. Here is the way I would like to do it (since it uses less lines and probably more efficient).

#!/usr/bin/perl
use warnings; use strict;
use CGI qw( :standard);
my $query = CGI->new;

# Process an HTTP request
my $user = $query->param('first_name');

# process $user... for example:
my $foo = "Foo";
my $str = $user . $foo;

print "Content-type:text/html\r\n\r\n";
print "<html>";
print "<head>";
print "<title>Hello - Second CGI Program</title>";
print "</head>";
print "<body>";
print "<h2>Hello $str - Second CGI Program</h2>";
print "</body>";
print "</html>";

1;

Here's a way I read from a tutorial and makes more sense to me:

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

my ($buffer, @pairs, $pair, $name, $value, %FORM);
# Read in text
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
if ($ENV{'REQUEST_METHOD'} eq "POST") {
   read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
}else {
   $buffer = $ENV{'QUERY_STRING'};
}
# Split information into name/value pairs
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%(..)/pack("C", hex($1))/eg;
    $FORM{$name} = $value;
}
my $user = $FORM{first_name};

# process $user... for example:
my $foo = "Foo";
my $str = $user . $foo;

print "Content-type:text/html\r\n\r\n";
print "<html>";
print "<head>";
print "<title>Hello - Second CGI Program</title>";
print "</head>";
print "<body>";
print "<h2>Hello $str - Second CGI Program</h2>";
print "</body>";
print "</html>";

1;

Both of these don't work properly BTW. When I click on the submit button on the HTML page, it just links me to the script instead of passing the variable, processing it, and printing out the HTML page.

6
  • on which server? apache? Commented May 4, 2013 at 19:47
  • I'm using Komodo Edit to write and view the HTML and an SSH server to test the final results. I've never used apache before and I don't have it installed, unfortunately. Commented May 4, 2013 at 19:50
  • 2
    You absolutely should not use the buggy parameter-processing code in your second example. The CGI module does this correctly. What is actually getting sent to your script? What happens if you dump all the params to the error log, for example? Commented May 4, 2013 at 19:53
  • but you need some kind of web server where you test that cgi script ... what are you using? Commented May 4, 2013 at 19:53
  • @cooldood3490, if you're not using a web server, how are you running this? Commented May 4, 2013 at 19:54

3 Answers 3

1

this line:

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

should be:

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

or better:

print $query->header;

Also, ensure your web server was well configurated for CGI. And, if you have enough time, use a modern web application approach, there are many frameworks that may be better than CGI (Dancer, Mojolicious, OX, ...)

Sign up to request clarification or add additional context in comments.

2 Comments

thanks Miguel. I made the changed as you said. I just installed apache. Now I'm gonna try to figure out how to run and test the cgi script using the apache server. I'll accept a best answer when I confirm that it works.
I'm getting the same error. When I click submit on the HTML page. It linked me to the perl cgi script instead of processing the input and displaying an HTML page. i.imgur.com/9WEm4uP.png
1

I see your using CGI 'standard', no need really to initiate a CGI->new unless you just wanted to, also you said less lines, you could just do something like this.

use strict;
use warnings;
use CGI qw( :standard );

my $user = param('first_name') || q/foo/;

print header, 
      start_html(-title => 'Hello'), h1('Hello ' . $user), end_html;

6 Comments

I'm getting the same error. When I click submit on the HTML page. It linked me to the perl cgi script instead of processing the input and displaying an HTML page. i.imgur.com/9WEm4uP.png
What does your html for your <form> data and submit look like?
<FORM action="name.cgi" method="POST"> Name: <input type="text" name="first_name"> <input type="submit" value="Submit">
First try using the full url to the script file, <form action="full/path/to/name.cgi", also do you have the write permissions set? chmod 0755?
Although sounds like apache is not configured to run .cgi files. Have you configured your web server yet?
|
1

You need to edit your httpd.conf with something like this.

AddHandler cgi-script cgi pl
<Directory /path/to/cgi/files>
Options +ExecCGI
</Directory>

If you are running this locally you could create a folder named public_html in your home directory structure and set this to run your scripts, you would just have to configure that also mapping it to that location.

2 Comments

thanks, I made the changes to the httpd.conf file. I'm getting this now: i.imgur.com/UDGGQHW.png even after changing permissions to chmod 0755. It wouldn't work before when I changed the html to the full path; I just have name.cgi in the same directory as the name.html. In any case, I'm getting closer.
The rule here is that Apache requires execute access to all folders in the path in order to serve files. Without this, you'll get a HTTP 403 (forbidden). It looks like you have your script in the main folder, you want to put it in your publc_html/cgi-bin or wherever your public directory is.

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.