how to pass a variable from an HTML form to a perl cgi script? -


i use html form pass variable perl cgi script can process variable, , print out on html page.

here html code: http://jsfiddle.net/wtvq5/.

here perl cgi script links html. here way (since uses less lines , more efficient).

#!/usr/bin/perl use warnings; use strict; use cgi qw( :standard); $query = cgi->new;  # process http request $user = $query->param('first_name');  # process $user... example: $foo = "foo"; $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 way read tutorial , makes more sense me:

#!/usr/bin/perl use warnings; use strict;  ($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 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; } $user = $form{first_name};  # process $user... example: $foo = "foo"; $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 btw. when click on submit button on html page, links me script instead of passing variable, processing it, , printing out html page.

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 web server configurated cgi. and, if have enough time, use modern web application approach, there many frameworks may better cgi (dancer, mojolicious, ox, ...)


Comments

Popular posts from this blog

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -