代码之家  ›  专栏  ›  技术社区  ›  John

单选按钮提交时转到其他文件

  •  -6
  • John  · 技术社区  · 8 年前

    我在写一个PerlCGI脚本。我的 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 回复  |  直到 8 年前
        1
  •  1
  •   Borodin    8 年前

    #!/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();
    }
    
        2
  •  0
  •   Ron Bergin    8 年前