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

Im a bit of a Perl newbie and I keep going round in circles. I want to create a drop down menu of 'Month Year ' combinations, the HTML would look like this:
<option selected value="Select a month">Select a month</option> <option value="0305">March 2005</option> <option value="0405">April 2005</option> <option value="0505">May 2005</option> <option value="0605">June 2005</option> <option value="0705">July 2005</option></select>
I want to pass the value the user selects in this drop down to a variable so that I can use it in Perl to look for a file with a name that correponds to the selected value and if it finds the file, to open it. I'm trying to create the drop down field like this:
my %month = ( "March 2005" => "0305", "April 2005" => "0405", "May 2005" => "0505", "June 2005" => "0605", "July 2005" => "0705", ); print option_list(\%month); sub option_list { my $options = shift; return "<select>\n" . (join '', map{ qq!<option value="$options->{$_}">$_</option>\n! } keys %$options ) . "</select>\n"; }
I've got the subroutine looking for the filename and opening it working, what I can't get to work is: 1. How to get the selected value out of the Drop Down Menu 2. How to get the Drop Down month to display in month order. (They currently display in this order in the list: June 2005, March 2005, July 2005, May 2005, April 2005 Can anyone help?

20050317 Edit by castaway: Changed title from 'Extracting selelcted values from HTML Select Menus'

Replies are listed 'Best First'.
Re: Extracting selected values from HTML Select Menus
by jhourcle (Prior) on Mar 14, 2005 at 12:31 UTC
    1. How to get the selected value out of the Drop Down Menu

    You have to give the select a name, so that the browser will send it. In your CGI backend, you then check what value has been sent with that name. So, you need to change the line

    "<select>\n" .

    to something like:

    "<select name='date'>\n".

    See the forms section of the HTML specifications for how to send it, and the CGI or CGI::Lite modules on how to receive it.

    2. How to get the Drop Down month to display in month order.

    You just need to tell the program what order to print the values -- keys will return in what seems to be a random order (it isn't, but it's not an order that's useful for this). You'll want to change the lines:

    (join '', map{ qq!<option value="$options->{$_}">$_</option>\n! } keys %$options ) .

    to something like:

    (join '', map{ qq!<option value="$options->{$_}">$_</option>\n! } sort sort_function keys %$options ) .

    and add a function sort_function that can correctly order your values. See sort for information about writing such a function.

Re: Extracting selected values from HTML Select Menus
by bradcathey (Prior) on Mar 14, 2005 at 12:09 UTC

    I might be stating the obvious, and forgive me, but why don't you just hardcode this in HTML or HTML::Template? Unless I'm totally misunderstanding what you are trying to do, I think you are trying to pound in a tack with sledgehammer. It's okay to use HTML.


    —Brad
    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
      Well I could do, but forgive me for being dim, how would I then find out the selected value in Perl? Don't forget that I then want to use that value to either a:) open a file which correcpsonds to that value or b:)upload that file depending on whether the file exists already or not.

        Are you using CGI?

        HTML: <form action="filecheck.pl" method="post"> <select name="filename"> <option value=... </select> </form> PERL: use CGI; my $query = new CGI; my $filename = $query->param('filename'); do stuff....

        Am I tracking or not?


        —Brad
        "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
Re: Extracting selected values from HTML Select Menus
by punkish (Priest) on Mar 14, 2005 at 15:11 UTC
    here is the browser/server interaction --

    On the browser you have a form (btw, your syntax is wrong)

    <form> <select name="month" size="1"> <option value="">Select a month</option> <option value="0305">March 2005</option> <option value="0405">April 2005</option> <option value="0505">May 2005</option> <option value="0605">June 2005</option> <option value="0705">July 2005</option> </select> <input type="submit"> </form>

    The user selects a month and hits submit. That action sends a form variable called month back to the server. Perl on the server grabs the form variable and stuffs it in a var like so --

    my $month = $cgi->param('month'); # You are using CGI or CGI::Simple, are you not? # now do whatever you want with that $month

    You want to actually generate the above form using Perl.

    # here is one of the million ugly ways of doing this. # The better way would be to use a templating system. my @months = qw(03March 04April 05May 06June 07July); print makeform(); sub makeform { my $form = "<select name='month' size='1'>\n"; $form .= "<option value=''>Select a month</option>\n"; for (@months) { $_ =~ /(\d\d)(.*)/; $form .= "<option value='" . $1 . "05'>" . $2 . "2005</option>\n"; } $form .= "</select>\n"; return $form; }

    yeewww. I haven't done that kind of code in a long time. Made me feel kinda dirty. I don't think I have written a web app without HTML::Template in 3 years.

    Update: corrected stupid typos and mistakes in bad, ugly code. Reason #467 to use a templating system.

    --

    when small people start casting long shadows, it is time to go to bed
      Oops I may have missed out some important information. This my code now:
      use CGI; use Win32::ODBC; use Time::Local; if (!($db=new Win32::ODBC($DSN))) { print "Error connecting to Database\n"; print "Error: " . Win32::ODBC::Error() . "\n"; } $SqlStatement = "SELECT StaffNo FROM FOSInput WHERE Username=\'$ntuser +'\)"; if ($db->Sql($SqlStatement)) { print "SQL failed.\n"; print "Error: " . $db->Error() . "\n"; if ($DebugMode) { print "<HR>$SqlStatement<HR>"; # Debug } } else { while($db->FetchRow()) { print "<form><select name=\"month\" size=\"1\">\n"; print "<option value=\"\">Select a month</option>\n"; print "<option value=\"0305\">March 2005</option>\n"; print "<option value=\"0405\">April 2005</option>\n"; print "<option value=\"0505\">May 2005</option>\n"; print "<option value=\"0605\">June 2005</option>\n"; print "<option value=\"0705\">July 2005</option>\n"; print "</select><input type=\"submit\"></form>\n"; print "At end of Input field\n"; my $filename= $cgi->param('month'); print "$filename\n"; } }
      I've gone for hard coding the drop down but when I select a month, the page just kind of refreshes, I get the 'At end of Input field' message but $filename isn't printed. What gives?
        I've just switched CGI errors on to screen and this is what I get: Can't call method "param" on an undefined value in my $filename= $cgi->param('month'); I thought 'month' was defined in the hard coding? I'm confused now! Help!