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
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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>
 </select>

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 CGI.pm 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 CGI.pm in an application, I just add in my own collect_form subroutine:

Code:
sub collect_form {

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

if ($ENV{"REQUEST_METHOD"} eq "POST") {
       read(STDIN,$buffer,$ENV{"CONTENT_LENGTH"});
       $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;
               }
       }

%form;

}


and then in my main code (to answer your question) I would write
Code:
%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 CGI.pm, and its simple to use too.

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


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


jfp.


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.

jfp



This page is a thread posted to the opentalk forum at www.opentalk.org.uk 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., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho