My first posted code snippet. Sorry if I leave anythign out, include anything extraneous, etc.
This snippet will generate the Javascript for 2 popup menus, the 2nd of which has varying options depending on the value of the first. The form in this example is named 'your_form', the first popup_menu is named 'category' and the second is named 'subcategory'. The %categories hash should have keys thats are the categories and values that are arrays of subcategories.
I tried to make it as neat as possible. It was formatted in an editor with an 80-character width.
UPDATE: Made some changes suggested by davidrw
# Sample categories hash, courtesy of davidrw my %categories = ( A => [ 'B', 'Z' ], C => ['D', 'E'] ); my $category = param('category') || ''; my $subcategory = param('subcategory') || ''; # Generate javascript print <<EOF; <script language="javascript"> function catChange(newCat) { with(document.your_form.subcategory) { options.length=0; switch(newCat) { EOF foreach $c (keys %categories) { print <<EOF; case "$c": EOF my $i = 0; foreach my $s (@{$categories{$c}}) { # Keeps menu options when page reloads # Only matters when form target is the script itself my $opts_params = ($s eq $subcategory && $c eq $category) ? ',true,true' : ''; print <<EOF; options[$i]=new Option("$s" $opts_params); EOF ++$i; } print "break;\n"; } print <<EOF; break; EOF } print <<EOF; case "Category": options[0]=new Option("Please choose a category"); break; } } } </script> EOF # Well, now wasn't that fun?! # Beginning of form print start_form(-name=>'your_form'); print p(popup_menu(-name=>'category', -values=>['Category',sort keys %categories], -onChange=> 'catChange(this.options[this.selectedIndex].val +ue);'), popup_menu(-name=>'subcategory', -values=>['Please choose a category']));
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Double popup_menu
by davidrw (Prior) on Aug 10, 2005 at 13:09 UTC | |
by wink (Scribe) on Aug 11, 2005 at 00:32 UTC |