简体   繁体   English

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

[英]Using perl CGI.pm through multiple subroutines

So I am working on a school project where I have been asked to write perl cgi script to create a social net site. 因此,我正在一个学校项目中工作,该项目要求我编写perl cgi脚本来创建一个社交网站。

I have created multiple subroutines for different pages, but my problem is that when I try to execute each subroutine individually, it works fine, but when i try to run it though some other subroutine, it just redirects me to my first page. 我已经为不同的页面创建了多个子例程,但是我的问题是,当我尝试分别执行每个子例程时,它可以正常工作,但是当我尝试通过其他子例程运行它时,它会将我重定向到我的第一页。

For eg : This is my New User page code : 例如:这是我的新用户页面代码:

    #!/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();

if I try to call the same subroutine with a subroutine "first" and click the "Save" button, it redirects me to the "First" page through which I called the new_user. 如果我尝试使用子例程“ first”调用相同的子例程,然后单击“保存”按钮,它将重定向到“ First”页面,通过该页面我调用了new_user。

Any help will be appreciated. 任何帮助将不胜感激。

Thanks 谢谢

Your program has the following structure: 您的程序具有以下结构:

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

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

The problem is that the input form doesn't set pararm('New User') , so you don't end up in new_user when you click Save . 问题在于输入表单未设置pararm('New User') ,因此当您单击Save时您不会以new_user结尾。


Solution 1: 解决方案1:

Add a hidden input to the input form with name New User and value 1 . 将隐藏的输入添加到名称为New User且值为1的输入表单中。


Solution 2: 解决方案2:

Change 更改

if (param("New User"))

to

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

Solution 3: 解决方案3:

Change the structure of the program to 将程序的结构更改为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM