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

I'd like to write a script that will create drop down lists (HTML selects) in HTML based on information stored in a database. In my db table i have 2 columns (option name and value) and there are 10 rows. the value column holds either a '0' or '1' which will determine wether or not 'on' or 'off' is selected in the html select fields. Can you guys help me out?

Replies are listed 'Best First'.
Re: drop down menus
by gregorovius (Friar) on Sep 21, 2000 at 11:39 UTC
    This would be the core of it if you're using Apache::ASP or Perlscript on IIS. You still need to put your table in the %vals hash (using DBI) and also do formatting and print the option names by each combo box. Doing it with CGI.pm should also be easy, so take a look at that module's documentation if you don't have ASP.
    <% foreach (sort keys %vals) { %> <select name="<%= $_ %>"> <option value="1"<%= ($vals{$_}) ? ' selected' : '' %>>On </option> <option value="0"<%= ($vals{$_}) ? ' selected' : '' %>>Off </option> </select> <% } %>
    You may also want to look at HTML::Embperl, which has a feature to automatically select combo box items (or fill in any other form elements) for you.
Re: drop down menus
by Michalis (Pilgrim) on Sep 21, 2000 at 14:56 UTC
    If you use CGI.pm and IF you have assigned the values of the db in a hash named (for example) %myhash then what you want is :
    start_form(-method=>'post', -action=>'youractionhere'), popup_menu(-name=>'yournamehere', -values=>\%hash), end_form;
    you may also use a 'default' value. (man CGI.pm and look for popup_menu). Needless to say that the above code won't work on its own (it needs a new CGI and much more code, which I guess you know how to create).
      -values needs an array reference (not an hash) so the right sintax is
      -values=>[sort keys %hash]
        Sorry, you're simply wrong.. What he needs is a hash reference: I copy/paste from the CGI.pm documentation: (in http://stein.cshl.org/WWW/software/CGI/cgi_docs.html)
        If you pass a HASH reference, the keys will be used for the menu values, and the values will be used for the menu labels.
Re: drop down menus
by cianoz (Friar) on Sep 21, 2000 at 15:38 UTC
    use CGI; my $q= new CGI; my Svalues = ['a', 'b', 'c']; my $defaults = ['a','c']; #i suppose you know how to fill these print $q->start_form(); print $q->scrolling_list( -name=>'LIST', -values=>$values, -multiple=>1, -defaults=>$defaults ); print $q->end_form(); ##you can fetch selected items with ## my @selected = $q->param('LIST');
    update: i notice you asked for dropdown menu, not scrolling lists
    so you should write:
    print $q->popup_menu( -name=>'LIST', -values=>$values, -default=>'single value' );
    remember if you use popup_menu you can select just a single item!