单选按钮转到提交时的另一个文件

时间:2018-06-10 13:10:39

标签: html perl

我正在写一个Perl CGI脚本。我的index页面应在表单中显示一组单选按钮,并根据所选按钮转到另一个页面/文件。

我无法找到正确的方法。

这是我的代码:

#!/usr/bin/perl

print "<!DOCTYPE html>";
print "<html>";
print "<body>";
print "<h1> Welcome to Magnum </h1>";
print "<h2> System Check </h2>";
print "<h2> -------------------- </h2>";

print "<form method='post'>";
print "<input type='radio' name='option' value='process' formaction='/home/john/www/process'/ > View My Process<$
print "<input type='radio' name='option' value='calendar'> View Calendar <br>";
print "<input type='radio' name='option' value='location'> View my location coordinates<br> Latitude and Altitud$
print "<input type='radio' name='option' value='users'> View all users connected on my System<br>";
print "<input type='radio' name='option' value='find'> Find a file or Directory<br>";

print "<input type='submit' name='submit' value='Submit'> <br>";


print "</form>";
print "<h2> -------------------- </h2>";
print "</body>";
print "</html>";

它必须像这样写,因为它是我们被教导的方式。 如果答案涉及的东西太不同,那么它将无济于事,

2 个答案:

答案 0 :(得分:1)

这里有一些可能有帮助的高级代码。

#!/usr/bin/perl

use strict;
use warnings;

# Use functions from CGI.pm to make your life easier
use CGI qw[header redirect param];

# Hash containing valid redirection values.
my %valid_redirects = map { $_ => 1 } qw[process calendar location
                                       users find];

# Get the chosen option
my $option = param('option');

# If we've a) got an option and b) it's a valid option
# then redirect to the chosen page.
if ($option and $valid_redirects{$option}) {
  # You'll need to write a redirect_to() subroutine
  print redirect_to("$option.html");
} else {
  # If we don't have a valid redirection option, display the form.
  # You'll need to write an html_form() subroutine
  print header;
  print html_form();
}

答案 1 :(得分:0)

这是一个建议的解决方案。

为每个单选按钮选项创建单独的子例程。使用这些subs创建一个调度表。根据表单提交执行相应的调度表子例程。