通过多个子程序使用perl CGI.pm

时间:2016-11-01 03:42:24

标签: html perl cgi.pm

所以我正在开展一个学校项目,在那里我被要求编写perl cgi脚本来创建一个社交网站。

我为不同的页面创建了多个子程序,但我的问题是,当我尝试单独执行每个子程序时,它工作正常,但是当我尝试通过其他一些子程序运行它时,它只是将我重定向到我的第一页。

例如:这是我的新用户页面代码:

    #!/usr/bin/perl -w
    use CGI qw/:all/;
    use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
    my $cgi = new CGI;

    sub main() {
            print page_header();

            warningsToBrowser(1);

            first();

            page_trailer();
        }


    sub page_header {
            return header(-charset => "utf-8"),
                start_html(-title => 'Matelook',-style => "matelook.css"),
                body({-background =>"background.jpg"}),
                div({-class => "matelook_heading"}, "matelook");
        }

    sub page_trailer {
            my $html = "";
            $html .= join("", map("<!-- $_=".param($_)." -->\n", param())) if $debug;
            $html .= end_html;
            return $html;
        }

    sub new_user {
                  $fullname = param("fullname") || '';
                    $newun = param("newun") || '';
                    $newpass = param("newpass") || '';
                    $email = param("email") || '';
                    $program = param("program") || '';

                if(param("Save")){
                    $data = "full_name=$fullname \n 
                         username=$newun \n
                         password=$newpass \n
                         email=$email \n
                         program=$program \n";


                        $location = "dataset-medium/$newun";
                        mkdir $location, 0755;  
                        open($f,'>',"$location/user.txt");
                        print $f $data;
                        print $data,"\n";
                        print "Data Saved !! ";

                }

                else{
                     print start_form,"\n";
                        print "Enter Full Name :\n", textfield("fullname");
                        print "New Username :\n", textfield('newun'),"\n";
                        print "New Password :\n", textfield('newpass'),"\n";
                        print "Email:\n",textfield('email'),"\n";
                        print "Program:\n",textfield('program'),"\n";
                        print submit({-name =>"Save",-class => "matelook_button"});
                        print end_form,"\n";       
                }
            }

    sub first{
            if(param("New User")){
                new_user();
            }
            else{
                print start_form,"\n";
                   print submit({-class => "matelook_button_first ",-name => 'New User'}),"\n";
                print end_form,"\n";
            }
        }
    main();

如果我尝试使用子程序“first”调用相同的子程序并单击“保存”按钮,它会将我重定向到我调用new_user的“First”页面。

任何帮助将不胜感激。

由于

1 个答案:

答案 0 :(得分:1)

您的程序具有以下结构:

sub new_user {
   if (param("Save")){
      save();
   } else {
      show_input_form();
   }
}

if (param("New User")){
   new_user();
} else {
   show_menu();
}

问题是输入表单没有设置pararm('New User'),因此当您点击new_user时,您不会在Save结束。

解决方案1:

在名称为New User且值为1的输入表单中添加隐藏的输入。

解决方案2:

更改

if (param("New User"))

if (param("New User") || param('Save'))

解决方案3:

将程序结构更改为

if (param("New User")){
   show_input_form();
}
elsif (param("Save")){
   save();
}
else {
   show_menu();
}