0

所以我正在做一个学校项目,我被要求编写 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”调用相同的子程序并单击“Save”按钮,它会将我重定向到我调用 new_user 的“First”页面。

任何帮助将不胜感激。

谢谢

4

1 回答 1

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使用 name和 value向输入表单添加隐藏输入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();
}
于 2016-11-01T04:35:54.190 回答