简体   繁体   中英

perl-cgi : how to retrieve the selection of a form?

could someone please tell me how can i determine what was the user's selection given a drop-down menu in the following piece of code? what am i doing wrong?

#!/usr/bin/perl -wT

use     strict ;
use     warnings ;
use     CGI ;
use     CGI::Carp qw(warningsToBrowser fatalsToBrowser) ;


my      $cgi = new CGI ;
print   $cgi->header(-type=>"text/html", -charset=>"utf-8") ;
print   $cgi->start_html(-title=>"Data Analysis | hellas online") ;

my %labels = (  "yes"   =>      "For Sure!",
                "no"    =>      "Not for me.",
                "maybe" =>      "Maybe So."     ) ;    
print $cgi->popup_menu( -name           => "your_answer",
                        -values         => ["yes", "no", "maybe"],
                        -default        => "yes",
                        -labels         => \%labels     ) ;
my @selected = $cgi->param("your_answer") ;
foreach my $i (@selected)
{
        print $cgi->$i."\n" ;
}
print   $cgi->end_html ;

the... print $cgi->$i."\n"; ... statement does not display anything?!

thank you.

You use the wrong syntax: Not print $cgi->$i."\n"; but print $i."\n";

What happens is simple:

$a = "abc"
print $cgi->$a ."\n"

will look for a member "abc" of $cgi

I am writing this on a hunch, so it may well not be correct, but hopefully it will serve as some sort of guide as to where to start looking: I think the problem is that, if the code provided really is all you've got, the website is static - ie there is no possibility of really selecting something. Once the user makes a change to what is currently displayed in the dropdown menu your script doesn't notice, as it's already done its job and the last line has already been executed. The change in the dropdown menu does not get passed on (or even noticed) by the web server, and thus @selected will remain an empty list (that said, I don't know why @selected does not at least contain your default selection, but perhaps I'm overlooking something here).

On a side note, you could also change @selected to $selected , as your dropdown menu does not allow multiple selections anyway.

You never create a form, so the value of the popup_menu can never be submitted. You need to put your form controls inside a form.

$cgi->param will have data to fetch only after the form data has been submitted (you'll need a submit button too) back to the server from the browser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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