如何制作一个下拉列表来接受perl cgi脚本中的用户输入值
我有一个perl cgi脚本,它有一个简单的下拉列表,供用户为字段meterSize选择值 但是现在我有了一个新的要求,允许用户在下拉列表中缺少值时输入一个值 可能吗 你能告诉我,如果我能做到这一点与我目前的设计或提供任何指针?链接?提前谢谢 代码-2是下拉列表的子例程实现。代码1是我调用子程序的方式 解释: meterSize来自一个db查询,并传递给子例程,该子例程从已知的值列表中为其实现下拉列表 代码-1: $value=drop_down_mSize(“米大小”、“米大小”、$job->{meterSize}) 代码-2:如何制作一个下拉列表来接受perl cgi脚本中的用户输入值,perl,cgi,Perl,Cgi,我有一个perl cgi脚本,它有一个简单的下拉列表,供用户为字段meterSize选择值 但是现在我有了一个新的要求,允许用户在下拉列表中缺少值时输入一个值 可能吗 你能告诉我,如果我能做到这一点与我目前的设计或提供任何指针?链接?提前谢谢 代码-2是下拉列表的子例程实现。代码1是我调用子程序的方式 解释: meterSize来自一个db查询,并传递给子例程,该子例程从已知的值列表中为其实现下拉列表 代码-1: $value=drop_down_mSize(“米大小”、“米大小”、$job->
sub drop_down_mSize {
my $local_label = shift;
my $local_name = shift;
my $db_eq_val = shift;
my %local_var = ( "15mm" => "15mm", "20mm" => "20mm", "25mm" => "25mm", "40mm" => "40mm", "50mm" => "50mm", "80mm" => "80mm", "100mm" => "100mm", "125mm" => "125mm", " " => " ", NULL => "NULL");
my $return;
$return .= "<td>$local_label</td><td>";
$return .= "<select name = '$local_name' id = '$local_name'>";
foreach(sort keys%local_var)
{
if($db_eq_val eq $_)
{
$return .= "<option selected value=\"$_\">$local_var{$_}</option>";
}
else
{
$return .= "<option value=\"$_\">$local_var{$_}</option>";
}
}
$return .= "</select>";
$return .= "</td>";
return $return;
}
子下拉菜单\u mSize{
我的$local_标签=移位;
我的$local_name=shift;
我的$db_eq_val=shift;
我的%局部变量=(“15mm”=>“15mm”,“20mm”=>“20mm”,“25mm”=>“25mm”,“40mm”=>“40mm”,“50mm”=>“50mm”,“80mm”,“100mm”=>“100mm”,“125mm”=>“125mm”,“NULL=”>“NULL”);
我的$return;
$return.=“$local_label”;
$return.=“”;
foreach(排序键%local\u var)
{
如果($db_eq_val eq$)
{
$return.=“$local_var{$}”;
}
其他的
{
$return.=“$local_var{$}”;
}
}
$return.=“”;
$return.=“”;
return$return;
}
但是现在我有一个新的要求,允许用户输入
值(如果下拉列表中缺少)
可能吗
当然:
创建_form.pl:
#!/usr/bin/env perl
use strict;
use warnings;
use CGI;
use CGI::Carp qw{ fatalsToBrowser };
#Starting data:
my $local_label = 'Choose a size:';
my $local_name = 'size_select';
my $db_eq_val = '20mm';
my @sizes = ( "15mm", "20mm", "25mm");
my $cgi = CGI->new;
print $cgi->header,
$cgi->start_html('My Size Page'),
$cgi->start_form(
-method => 'post',
-action => '/cgi-bin/process_form.pl',
),
$cgi->div(
$cgi->label({-for=>$local_name}, $local_label),
$cgi->popup_menu( #select() is a perl butilin funciton, so 'popup_menu" is the function name
-name => $local_name,
-id => $local_name,
-values => \@sizes,
-default => $db_eq_val #If the default isn't in options, then first option will be selected
),
),
$cgi->div(
$cgi->label({-for=>'size_text_box'}, "If desired size isn't up there, enter it here:"),
$cgi->input({
-type => 'text',
-id => 'size_text_box',
-name => 'size_text_box'
}),
),
$cgi->submit('Submit', 'submit'),
$cgi->end_form,
$cgi->end_html();
#!/usr/bin/env perl
use strict;
use warnings;
use CGI;
use CGI::Carp qw{ fatalsToBrowser };
use Template;
my $local_label = 'Choose a size:';
my $local_name = 'size_select';
my $db_eq_val = '20mm';
my @sizes = ( "15mm", "20mm", "25mm");
my $vars = {
select_label => $local_label,
select_name => $local_name,
default => $db_eq_val,
sizes => \@sizes,
};
my $tname = 'templates/form.html';
my $template = Template->new({
INCLUDE_PATH => '/Users/7stud/perl_programs/cgi_projects/1proj',
});
$template->process($tname, $vars)
or die "*MY* template process failed: ", $template->error(), "\n";
下面是代码创建的html:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<head>
<title>My Size Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<form method="post" action="/cgi-bin/2cgi.pl" enctype="multipart/form-data">
<div><label for="size_select">Choose a size:</label> <select name="size_select" id="size_select">
<option value="15mm">15mm</option>
<option selected="selected" value="20mm">20mm</option>
<option value="25mm">25mm</option>
</select></div><div><label for="size_text_box">If desired size isn't up there, enter it here:</label> <input name="size_text_box" type="text" id="size_text_box" /></div><input type="submit" name="Submit" value="submit" /></form>
</body>
</html>
然而,现代的方法是使用模板。以下是模板工具包:
创建_form.pl:
#!/usr/bin/env perl
use strict;
use warnings;
use CGI;
use CGI::Carp qw{ fatalsToBrowser };
#Starting data:
my $local_label = 'Choose a size:';
my $local_name = 'size_select';
my $db_eq_val = '20mm';
my @sizes = ( "15mm", "20mm", "25mm");
my $cgi = CGI->new;
print $cgi->header,
$cgi->start_html('My Size Page'),
$cgi->start_form(
-method => 'post',
-action => '/cgi-bin/process_form.pl',
),
$cgi->div(
$cgi->label({-for=>$local_name}, $local_label),
$cgi->popup_menu( #select() is a perl butilin funciton, so 'popup_menu" is the function name
-name => $local_name,
-id => $local_name,
-values => \@sizes,
-default => $db_eq_val #If the default isn't in options, then first option will be selected
),
),
$cgi->div(
$cgi->label({-for=>'size_text_box'}, "If desired size isn't up there, enter it here:"),
$cgi->input({
-type => 'text',
-id => 'size_text_box',
-name => 'size_text_box'
}),
),
$cgi->submit('Submit', 'submit'),
$cgi->end_form,
$cgi->end_html();
#!/usr/bin/env perl
use strict;
use warnings;
use CGI;
use CGI::Carp qw{ fatalsToBrowser };
use Template;
my $local_label = 'Choose a size:';
my $local_name = 'size_select';
my $db_eq_val = '20mm';
my @sizes = ( "15mm", "20mm", "25mm");
my $vars = {
select_label => $local_label,
select_name => $local_name,
default => $db_eq_val,
sizes => \@sizes,
};
my $tname = 'templates/form.html';
my $template = Template->new({
INCLUDE_PATH => '/Users/7stud/perl_programs/cgi_projects/1proj',
});
$template->process($tname, $vars)
or die "*MY* template process failed: ", $template->error(), "\n";
根据报告:
调用process()方法来处理模板。第一
参数将输入模板指示为以下之一:
[% INCLUDE templates/header.html
title = "Results page"
%]
<form action="/cgi-bin/process_form.pl" method="post">
<label for="[% select_name %]">[% select_label %]</label>
<select id="[% select_name %]" name="[% select_name %]">
[% FOREACH size IN sizes %]
[% IF size == default %]
<option selected="selected" value="[% size %]">[% size %]</option>
[% ELSE %]
<option value="[% size %]">[% size %]</option>
[% END %]
[% END %]
</select>
<div>
<label for="size_text_box">If you didn't see the size above, enter one here:</label>
<input type="text" id="size_text_box" name="size_text_box">
</div>
<div>
<input type="submit" value="Submit">
</div>
</form>
[% INCLUDE templates/footer.html%]
模板/results.html:
[% INCLUDE templates/header.html
title = "Results Page"
%]
<div>You chose: [% chosen_size %]</div>
[% INCLUDE templates/footer.html %]
serverHTTP.py:
#python2:
import BaseHTTPServer
import CGIHTTPServer
PORT = 8000
handler = CGIHTTPServer.CGIHTTPRequestHandler
httpd = BaseHTTPServer.HTTPServer(("", PORT), handler)
print "serving at port", PORT
httpd.serve_forever()
'''
#python3:
import http.server
PORT = 8000
handler = http.server.CGIHTTPRequestHandler
httpd = http.server.HTTPServer(("", PORT), handler)
print("serving at port", PORT)
httpd.serve_forever()
'''
您只需将该服务器放在您想要提供文件服务的任何目录中(该目录将是服务器根目录/
,因此该目录中的文件路径将是/some_file.html
)。然后启动服务器:
$ python serverHTTP.py
默认情况下,将执行名为cgi-bin/
的子目录中的文件,您可以在浏览器中使用url向脚本发送请求,如下所示:
http://localhost:8000/cgi-bin/create_form.html
在html文件中,您将使用以下路径:
<form action="/cgi-bin/process_form.html" method="post">
我不相信直接使用HTML就可以做到这一点,但你可以看看jquery ui。仅供参考,CGI文档说:“CGI.pm中的所有HTML生成功能不再被维护。任何问题、错误或修补程序都将被拒绝,除非它们与根本性的页面呈现有关……其基本原理是,CGI.pm的HTML生成功能充其量只是一个模糊处理,充其量只是一个维护噩梦。您应该使用模板引擎来更好地分离关注点。“@ThisSuitesBlackNot,1)这些不是CGI文档。CGI文档没有这样的说法。2) op说他们在使用CGI,3)我正在制作一个模板示例,向op展示他们应该做什么,但是我选择的模板系统不会@!#$@!#!$工作可以1) 我链接到了2014年12月18日发布的CGI最新版本4.13的文档。它们与perldoc.perl.org上的文档不匹配,因为core中包含的CGI版本较旧。2) 我引用的部分没有说“不要使用CGI”,而是说“不要使用HTML生成函数”(尽管CGI正在使用)。3) 放松。我只是想给你一些你可能不知道的最近变化的信息。
$ python serverHTTP.py
http://localhost:8000/cgi-bin/create_form.html
<form action="/cgi-bin/process_form.html" method="post">