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

Hi,
How are some of you keeping the state of a select drop down box, specifically how to keep the choice made, and redraw the page?
I use perlfect for most perl/html, but inside of a select box option line, a tag seems to choke it. I realize that I probably need to do some kind of search and replace, but nothing seems to be working well. Any ideas?
  • Comment on How to keep the state of a drop down select box in perl

Replies are listed 'Best First'.
Re: How to keep the state of a drop down select box in perl
by perlmonkey (Hermit) on Apr 24, 2000 at 21:09 UTC
    The easiest way is to use CGI.pm. It will repopulate all your fields automatically when the page is redrawn after a submit. Try this code:
    #!/usr/bin/perl use CGI qw(:standard); $cgi = new CGI; print header(), start_html(); print start_form(-action=>$cgi->script_name()); print hidden(-name=>'PrevSelect', -value=>$cgi->param('Select')); %labels = ("foo"=>"Foo", "bar"=>"Bar", "baz"=>"Baz"); print popup_menu(-name=>'Select', -values=>[keys %labels], -default=>'baz', -labels=> \%labels); print submit(); print end_form(), end_html();
    It will preserve whatever is in the popup_menu (<SELECT>) selection after an action has been taken.
Re: How to keep the state of a drop down select box in perl
by BBQ (Curate) on Apr 24, 2000 at 20:55 UTC
    I do it the hard way...
    <select name="dropdown"> <option>Option 1</option> <option>Option 2</option> <option>Option 3</option> </select>
    and on the perl side...
    my @dropdown = ('Option 1','Option 2','Option 3'); foreach $op (@dropdown) { if ($op eq $in{'dropdown}) { # I use cgi-lib.pl still... $dropdown .= "<option selected>$op</option>\n"; } else { $dropdown .= "<option>$op</option>\n"; } } # later on insert $dropdown somewhere on you're # HTML response page...
    As I said, I'm sure there are easier approaches to this, but I still do it ye good ol' way...

    #!/home/bbq/bin/perl
    # Trust no1!
      for things like:
      <select name="dropdown"> <option>Option 1</option> <option>Option 2</option> <option>Option 3</option> </select>
      I usually use map:
      my @options = ( 'Option 1', 'Option 2', 'Option 3', ); @options = map { "<option>$_</option>" } @options; print '<select name="dropdown">', @options, "</select>\n";
        Yes, but... that doesn't actually output a selected option! :) The question was on how to keep the state of a dropdown box. I guess mine doesn't work either since I am only counting on a single select dropdown, and just looking over it occured to me that it would break under a multiple select.

        eeeek!

        #!/home/bbq/bin/perl
        # Trust no1!
Re: How to keep the state of a drop down select box in perl
by turnstep (Parson) on Apr 24, 2000 at 20:56 UTC

    I think you are asking about the SELECTED modifier to the OPTION tag. Don't hard code it, however, because it may change. Use a simple test to see whether or not to include it. In the example below, the variable $color has already been set. Note that is $color does not match anything, the browser will show the first OPTION as the selected one.

    @colors = ("red", "blue", "green", "cyan"); print "<SELECT NAME=\"MyColor\">\n"; for $x (@colors) { printf "<OPTION%s>$x", $color eq $x ? " SELECTED" : ""; } print "</SELECT>\n";

Re: How to keep the state of a drop down select box in perl
by httptech (Chaplain) on Apr 25, 2000 at 00:20 UTC
    I do it the easy way:
    print qq~ <select> <option>$populate <option>1 <option>2 <option>3 </select> ~;
    so you may end up with a duplicate entry in the HTML:
    <select> <option>2 <option>1 <option>2 <option>3 </select>
    but it works and it's a lot cleaner code on the perl side.
      Hey, that IS easier! I just wish I had clients that would let me do that! :o(

      BTW: Is the option tag valid html if you don't close it?

      Cheers!
Re: How to keep the state of a drop down select box in perl
by chipmunk (Parson) on Dec 11, 2000 at 02:34 UTC
    Well, I ended up doing a search of file contents to find cookie documentation. It's not in LWP nor in lwpcook. Instead, it's in the documentation for HTTP::Cookie and LWP::UserAgent (offsite links to search.cpan.org). The documentation is short on examples, but it should be enough to get you started.