Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Python, Lua and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, C, C++, Ruby, MySQL and Linux / Tomcat systems
How pass a variable from a form to a subroutine

Posted by ability (ability), 28 October 2002
I'm new at this but I have a variable, lets call it $var1 and I need a form to pass $var1 to a subroutine in a script.

The form has a drop down menu ie

<select size="1" name="var1">
   <option value="1">1</option>
   <option value="2">2</option>

How do I pass what ever value the user selects from the dropdown menu so that $var1= whatever the drop down menu value is??

Posted by admin (Graham Ellis), 29 October 2002
I'll assume you're using "CGI" - the common gateway interface.  If so, you might like to use a module such as to collect all the values from your form using the param method; personally, if this is the only thing that I would be doing with in an application, I just add in my own collect_form subroutine:

sub collect_form {

my $buffer;
my $field;
my @fof;
my %form;

       $form{"_method"} = "POST";
} else {
       $buffer = $ENV{"QUERY_STRING"};
       $form{"_method"} = "GET";

@fof = split(/&/,$buffer);
foreach $field(@fof) {
       my ($name,$value) = split(/=/,$field);
       $name =~ tr/+/ /;
       $name =~ s/%(..)/pack("C",hex($1))/eg;
       $value =~ tr/+/ /;
       $value =~ s/%(..)/pack("C",hex($1))/eg;
       if ($form{$name}) {
               $form{$name} .= "\n$value";
       } else {
               $form{$name} = $value;



and then in my main code (to answer your question) I would write
%els = collect_form();
$var1 = $els{"var1"};

CGI / getting values from form is a huge subject, but this example is a good general case that picks up all the elements from a form if you've used the GET or POST method when setting up the form.  I have an enhanced version that feeds cookie variables and environment variables into the same hash as well to avoid the need to keep recoding.  After all, what are subs for?

Posted by John_Moylan (jfp), 29 October 2002
I'm a big fan of, and its simple to use too.

use CGI;
my $q = CGI->new();
my $var1 = $q->param('var1');
print "$var1\n";

what if your html select box allowed multiple selections?
my @var1 = $q->param('var1'); # it can handle arrays too


Posted by John_Moylan (jfp), 29 October 2002
Oh, on a related note.......

Don't trust input from users, they mess with things you know.

Get used to using the -T switch in your cgi progs, it makes you 'untaint' user input.

I'll leave it to you to read up on it.


This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

Visitor Ranking 5.0 (5=excellent, 1=poor)

edit your own (not yet published) comments

Average page ranking - 5.0

© WELL HOUSE CONSULTANTS LTD., 2018: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • FAX: 01225 793803 • EMAIL: • WEB: • SKYPE: wellho