开发者

perl cgi form processing

I am working on a file and I am trying to understand how to process a form in hopes of passing a hidden field. for simplicity, lets say i want my scipt to simply show the value of the hidden field when it is first presented to the user, incremented by one, and after it is 'submitted', the new script displayed with the updated hidde开发者_高级运维n field. I am trying to gain insight on the explicit procedure so i can apply it to one of my current projects. I have searched the web but most examples simply confuse me, can anyone chime in?


Values submitted by the form can be retrieved using the CGI module (since you haven't shown any code, I don't know whether you're using CGI or attempting to handle the CGI interactions by hand; if you're doing it by hand, You're Doing It Wrong) and its param method.

Given the HTML form:

<form action='my_script.cgi' method=POST>
  <input type=hidden name=hidden_field value=1>
  <input type=submit>
</form>

You can retrieve the hidden value with (in my_script.cgi):

#!/usr/bin/env perl

use strict;
use warnings;

use CGI;

my $q = CGI->new;
my $hidden_value = $q->param('hidden_field');


You could write the value of the hidden field to a cookie. Each time you refresh or revisit the same webpage, your script can read the cookie into the hidden variable and increment it by one. The following example uses a variable instead of a hidden field in the form.

#!/usr/bin/perl
#countvisits.cgi
use strict;
use warnings;

use CGI qw(:standard -debug);
use CGI::Carp qw(fatalsToBrowser);

#declare variables
my ($count, $C_record);

#Create a new CGI object
my $cgi = new CGI;

#Read the cookie
#assign input to variable
$count=$cgi->cookie('count');

$count++;

#create cookie
$C_record = cookie(-name => "count",
                 -value => $count,
                 -expires => "6M");


#send cookie to browser
print header(-cookie => $C_record);

#create Web page
print "<HTML>\n";
print "<HEAD><TITLE>Jubilee Book Club</TITLE></HEAD>\n";
print "<BODY>\n";
print "<H1 ALIGN=center>Hello!<BR>\n";
print "You have been here $count times.</H1>\n";
print "</BODY></HTML>\n";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜