[lug] Perl CGI Question

Joseph McDonald joem at uu.net
Wed Aug 6 13:45:35 MDT 2003


Hey gang, 

Please forgive the long code paste. I've been racking my 
brain trying to figure out the best way to set an option value 
'selected' in a select query in a perl CGI. Best demonstrated
as a short working example.. 

I'd like to have the second page display the previously 
submitted value in the newly printed select query. The
way to do this would be to print:

     print qq|<OPTION VALUE="Apr" selected>Apr \n|;

in the return page, had Apr been selected. I just can't think 
of an easy way to do that. The only way I can think of doing 
it would be an if/else statement for each option value which 
seems ridiculous. Any ideas?

	--joey



#!/usr/local/bin/perl

use CGI qw(:standard);

my $date = param('month');

print header,
   start_html('A Simple Example'),
   h1('A Simple Example');

unless (param()) {

    print start_form;
    print qq| Enter the date: \n|;
    print qq|<SELECT NAME="month">\n|;
    print qq|<OPTION VALUE="">\n|;
    print qq|<OPTION VALUE="Jan">Jan \n|;
    print qq|<OPTION VALUE="Feb">Feb \n|;
    print qq|<OPTION VALUE="Mar">Mar \n|;
    print qq|<OPTION VALUE="Apr">Apr \n|;
    print qq|<OPTION VALUE="May">May \n|;
    print qq|<OPTION VALUE="Jun">Jun \n|;
    print qq|<OPTION VALUE="Jul">Jul \n|;
    print qq|<OPTION VALUE="Aug">Aug \n|;
    print qq|<OPTION VALUE="Sep">Sep \n|;
    print qq|<OPTION VALUE="Oct">Oct \n|;
    print qq|<OPTION VALUE="Nov">Nov \n|;
    print qq|<OPTION VALUE="Dec">Dec \n|;
    print qq|</SELECT> \n|;

    print submit(), end_form, hr;


}
else {
    print start_form;
    print "You selected $date\n<BR>", hr;

    print qq| Enter the date: \n|;
    print qq|<SELECT NAME="month">\n|;
    print qq|<OPTION VALUE="">\n|;
    print qq|<OPTION VALUE="Jan" >Jan \n|;
    print qq|<OPTION VALUE="Feb" >Feb \n|;
    print qq|<OPTION VALUE="Mar" >Mar \n|;
    print qq|<OPTION VALUE="Apr" >Apr \n|;
    print qq|<OPTION VALUE="May" >May \n|;
    print qq|<OPTION VALUE="Jun" >Jun \n|;
    print qq|<OPTION VALUE="Jul" >Jul \n|;
    print qq|<OPTION VALUE="Aug" >Aug \n|;
    print qq|<OPTION VALUE="Sep" >Sep \n|;
    print qq|<OPTION VALUE="Oct" >Oct \n|;
    print qq|<OPTION VALUE="Nov" >Nov \n|;
    print qq|<OPTION VALUE="Dec" >Dec \n|;
    print qq|</SELECT> \n|;

    print submit(), end_form, hr;
}

print end_html();




More information about the LUG mailing list