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']));

In reply to Double popup_menu by wink

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.