gdnew has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perlmonks.
Is there anyway to redirect the list of value in the drop down box into a perl script?
This perl script will read the value of the selection into a variable *input. For eg the dropdown box contain 3 value :
Class 1
Class 2
Class 3
When the user choose the class 1... it will redirect to another perl script prog2.pl which will read this "Class 1" as the value.The handling program prog2.pl is fine.
But I'm confused about how to redirect each entry in the list to the perlscript.
This is part of my code where I create my dropdown box:
print << "end_file"; <HTML> <HEAD> <TITLE>Classes Design</TITLE> </HEAD> <BODY LEFTMARGIN=50 BGCOLOR="#E3FFE3" VLINK="blue" LINK="blue" ALINK=" +blue"> <FORM name="menuform"> <TABLE CELLSPACING=10> <TR><TD> <U><B>Visualization classes 3D model structures</B></U> <P>Select classes <BR><SELECT name="class"> end_file &printClass(); sub printClass{ foreach my $element(@class){ if ($element ne ""){ print "<OPTION VALUE=\"$element\">"; print "$element\n"; } #end if element } #end foreach } #end printClass
Thanks so much.
Regards

gdnew

Replies are listed 'Best First'.
Re: autoredirect drop down box to perl files
by snowcrash (Friar) on Mar 12, 2002 at 07:55 UTC
    here's one script that outputs the popup menu _and_ handles the redirection. if you're not familiar with CGI.pm and it's methods for html generation, check out the CGI manpage.
    note that you can either use a submit button to submit the form, or the onChange parameter to the popup_menu (requires javascript to work => bad idea), that submits the form automatically when you choose a class.
    #!/usr/bin/perl use warnings; use strict; use CGI; my $q = CGI->new(); my %urls = ( 1 => "prog2.pl", 2 => "prog3.pl", 3 => "prog4.pl" ); my %labels = ( 1 => "Class 1", 2 => "Class 2", 3 => "Class 3" ); my $class = $q->param("class"); if ($class && $urls{$class}) { print $q->redirect($urls{$class}); } else { print $q->header, $q->start_html, $q->start_form; print $q->popup_menu( -name => "class", -values => [ keys %urls ], -labels => \%labels, # alternative to submit button below # -onChange => "submit()", ); print $q->submit("go", "GO!"); print $q->end_form, $q->end_html; }

    snowcrash
Re: autoredirect drop down box to perl files
by cjf (Parson) on Mar 12, 2002 at 07:26 UTC

    If I understand your question correctly, you can simply have the main script redirect the parameter to other scripts depending on its value.

    #!/usr/bin/perl -wT use strict; use CGI; my $q = new CGI; my $class = $q->param("class"); my %scripts = ( prog1 => "prog1.pl", prog2 => "prog2.pl", prog3 => "prog3.pl", ); if ( $scripts{$class} ) { print $q->redirect("$scripts{$class}?class=$class"); } else { error("You don't have permission to access $class"); } sub error { my $error = shift; print $q->header("text/html"), $q->start_html("Error"), $q->h1("Error"), $q->p("$error"), $q->end_html; exit; }
Re: autoredirect drop down box to perl files
by maverick (Curate) on Mar 12, 2002 at 07:29 UTC
    I'm not quite sure what you're asking for...here's two possible takes on it.

    1) You want to use a SINGLE perl script 'prog2.pl' and have it receive the current value of the select list.

    All you need to do is add a 'action="prog2.pl"' to the FORM tag. And then add a <input type=submit value="go"> tag to the bottom of the form.

    2) You want to use a different Perl script for each value in the select list.

    This one is a bit trickier. You're going to have to use a bit of javascript to set the 'action' tag in the form accordingly when the selection list changes.

    I suspect you mean the former and not the later... :) HTH

    /\/\averick
    perl -l -e "eval pack('h*','072796e6470272f2c5f2c5166756279636b672');"

Re: autoredirect drop down box to perl files
by Anonymous Monk on Mar 12, 2002 at 07:28 UTC
    Is there anyway to redirect the list of value in the drop down box into a perl script?
    Yes.
    But I'm confused about how to redirect each entry in the list to the perlscript.
    What exactly is confusing you?

    This is really a HTML issue, and this should be enough of a hint to help you solve your problem.

    How is it that you were able to diagnose this as a perl issue?

    How to read the friendly manual is always interesting reading.