Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

dynamic HTML pull down

by csuhockey3 (Curate)
on Nov 04, 2003 at 23:43 UTC ( [id://304578]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,

I have a very large form that is mailed to several aliases determined by mapping a country name to a region. I have a list of all countries and their respective regions in a text file formatted this way:

... namerica:united states samerica:brazil ...

So North America has an alias, South America has one and so on. In my CGI, I built a pull down from this list from that file and have a sub that maps to the correct regional email alias.

The problem: I need to call the sub that generates the pull down more than once. Here is some sample code to try and reiterate/illustrate what I am trying to accomplish

#the email part of the CGI ... Country: $param{country} ... #sub to create HTML pull down sub generateCountries{ my (@all_countries, $HTML_String); open(FILE, "<countries.txt") or die "cannot read file"; while(<FILE>){ if(/(.*):(.*)/i){ push @all_countries, $1; } } my $HTML_String = "\t" . '<select name = "country"> <option name="country" value="">-- Select a Country --</option +>'; for(my $i = 0; $i < $#all_countries; $i++){ $HTML_String .= "\t\t" . '<option value = "' . $all_countries +[$i] . '">' . $all_countries[$i] . "</option>\n"; } $HTML_String .= "\t</select>"; return $HTML_String; #the HTML form call to CGI that prints pull down <td> Country:</td><td colspan='2'> <?pp:CALL:ALL_COUNTRIES?></td></td>

Any suggestions on how to make this work with out having to write that whole sub over and over again (I need 4 pull downs), so I will end up with something more like this:

... Country : $param{country} Country1: $param{country1} Country2: $param{country2} ...
Thanks All

CSUhockey3

Replies are listed 'Best First'.
Re: dynamic HTML pull down
by blokhead (Monsignor) on Nov 05, 2003 at 00:12 UTC
    To answer your question directly, I would split off the file-reading bit from the HTML-generation bit (to avoid reading the file multiple times), then pass some arguments to your subroutine so it can be a little more flexible. Why make 4 nearly identical subroutines whose outputs differ in only 1 character? Pass in an argument!
    my @countries = do { my $fh; open($fh => "countries.txt") ? map { /(.*):(.*)/ ? $1 : () } <$fh> : (); }; my $dropdown = make_dropdown("dropdown", @countries); my $another = make_dropdown("another", @countries); sub make_dropdown { my ($form_name, @countries) = @_; ## generate and return a dropdown with <select name="$form_name"> }
    But a much better solution for your problem in general would be to just use the CGI modules's existing capabilities, including the (horribly-named) popup_menu routine which generates a dropdown. You are using CGI, right?
    use CGI 'popup_menu'; my $dropdown = popup_menu("dropdown", \@countries); my $another = popup_menu("another", \@countries, "USA"); ## set a de +fault
    The benefit of this is that you can easily set the selected value of the dropdown.. And it's already been written and tested, etc.

    blokhead

      Yes, I am using CGI. I like the idea of of popup_menu, I had not known about that. Thanks for the tips -- I think I will try that and also some things suggested by injunjoel.
Re: dynamic HTML pull down
by injunjoel (Priest) on Nov 05, 2003 at 00:56 UTC
    Greetings all,
    I am not entirely sure I understand the problem but here are my thoughts on how to get (according to my understanding) the desired results.
    why not create the list once and assign it to a string, then use regex string replacement for your various values;
    my $dd_country_str; unless(open(FILE, "<countries.txt")){ die "cannot read file"; }else{ $dd_country_str .= qq*<select name="countries">\n*; while(<FILE>){ if(/(.*):(.*)/i){ $dd_country_str .= qq*<option value="$1">$2</option>\n*; } } $dd_country_str .= qq*</select>\n*; close(FILE); } ###now call the dd_str you just built; my $first_dropdown = $dd_country_str; $first_dropdown =~ s/(value="$country_alias_you_want_selected")/$1 sel +ected="selected"/g; ###XHTML compliant print $first_dropdown; . . .
    Is that what you are looking for? It seemed from your original post you were going to be using the same list of countries each time, so I just created the dropdown list in a string and reused that each time.
    just a thought.
    Also you do not need to specify a name for each option within your select. The name of the select is what will be passed and assigned the value of whatever option is selected.
    -injunjoel
      I like the idea of one string, I am going to use that regardless to try and clean up how I access the text file that has the region and email aliases. Right now the aliases are hashed when I search the .txt file every time (bad). I made things much to difficult for myself. Also with this, the params would be $param{$1,3,3..} right -- the 'name' is not necessary?
        Greetings again,
        Sorry for the lag in response... work.
        Im not sure about your $param{$1,3,3...} question I think I would need to see more of your script to address that.
        However the 'name' is not necessary part was in reference to the line in your original post:
        <option name="country" value="">-- Select a Country --</option>
        in which case you do not need to give this option tag a name attribute since it is contained within the <select name="country"> drop down list.
        peace and blessings,
        -injunjoel

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://304578]
Approved by HyperZonk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-03-29 10:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found