Jea*_*ean 4 html forms perl cgi action
我试图根据不同的提交按钮将不同的操作分配给相同的html表单.
我可以这样做吗?
<FORM>
------
<INPUT type="submit" value="DoSomething" action="DoSomething.pl" method="POST">
<INPUT type="submit" value="DoSomethingElse" action="DoSomethingElse.pl" method="POST">
<FORM/>
Run Code Online (Sandbox Code Playgroud)
Mer*_*ovi 13
以防其他人发现此帖:
如果您使用的是HTML5,由于该formaction属性,现在可以更轻松了.此属性适用于input和的button元素,type="submit"并强制表单提交到formaction单击元素的属性中指定的位置.
然后,该属性的唯一缺点是Internet Explorer 9及更低版本不支持它,但使用一点JavaScript可以轻松克服此限制.
例:
<form method="post" action="go_default">
<input type="submit" value="Go Left" formaction="go_left" />
<input type="submit" value="Go Right" formaction="go_right" />
</form>
Run Code Online (Sandbox Code Playgroud)
对于IE 9及更低版本:
<script type="text/javascript">
$(function () {
var $submit = $('form [type="submit"][formaction]');
$submit.click(function() {
var $this = $(this),
action = $this.prop('formaction'),
$form = $this.closest('form');
$form.prop('action', action).submit();
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
Sin*_*nür 11
编号A形式只有一个action(action作为一个所述的属性形式,而不是提交按钮).
动作的目标可以根据表单中的值执行不同的操作.因此,您可能希望开始命名提交按钮.
在考虑编写和部署CGI脚本之前学习HTML.
<form method="POST" action="/cgi-bin/script">
<input type="submit" name="action" value="DoSomething">
<input type="submit" name="action" value="DoSomethingElse">
</form>
Run Code Online (Sandbox Code Playgroud)
另请注意,如果您希望国际化应用程序,则value根据提交按钮的值选择操作是一种失败策略,因为提交按钮是UA向人类显示的内容.
因此,script应根据其他一些输入元素的值来决定做什么.
例如,CGI :: Application查看run_mode参数.
或者,您可以为Alec建议的提交按钮使用不同的名称.在这种情况下,您需要通过浏览传递给脚本的参数的名称来检查按下了哪个提交按钮,恕我直言,这使得调度稍微麻烦.这也意味着它可能是有人将值传递所有提交按钮,您的脚本(不通过用户界面,但通过curl或wget或类似的计划.
例如,给定HTML
<form method="POST" action="/cgi-bin/script">
<input type="submit" name="submit_left" value="Go Left">
<input type="submit" name="submit_right" value="Go Right">
</form>
Run Code Online (Sandbox Code Playgroud)
以下是您的脚本可以处理表单提交的方式:
#!/usr/bin/perl
use strict; use warnings;
use CGI::Simple;
my $cgi = CGI::Simple->new;
my %dispatch = (
left => \&handle_left,
right => \&handle_right,
);
my @actions = grep s/^action_(right|left)\z/$1/, $cgi->param;
my $handler = \&handle_invalid_action;
if ( @actions == 1) {
my ($action) = @actions;
if ( exists $dispatch{ $action } ) {
$handler = $dispatch{ $action };
}
}
else {
$handler = \&handle_too_many_actions;
}
$handler->($cgi);
sub handle_left { }
sub handle_right { }
sub handle_invalid_action { }
# because it may indicate someone trying to abuse your script
sub handle_too_many_actions { }
Run Code Online (Sandbox Code Playgroud)