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

Dear Perl Monks,

Is it possible for Perl-CGI scripts to react to a user's selection on the form? I have a few popup menus on my form and when the user selects one entry in a popup form, I need to change the options in another.

Is it possible to do this in Perl itself or do I need to use Javascript? I am using code of the form:

print "<p><em>Enter Chip" ; print $query->popup_menu(-name=>'Chip', -values=>$arrRefC, -default=>'all'); print "<p><em>Enter Chip Rev</em> &nbsp "; print $query->popup_menu(-name=>'Chip Rev', -values=>$arrRefCR1, -default=>'all');
I get the desired functionality, except that, I need to change  $arrRefCR1 to, say,  $arrRefCR2 when the user changes his selection on the chip popup menu.

I would greatly appreciate any advice from the monks.

Thanks,
Ashish.

Replies are listed 'Best First'.
Re: Can Perl/CGI scripts respond to user selection?
by arden (Curate) on Mar 10, 2004 at 21:19 UTC
    I think your best choice for what you are asking for is javascript. Remember, Perl won't run anything on the local machine, only one the server end. The only way Perl would do what you want is if the user had to select their Chip, then submit it to the server, then the server could return a different form based on that choice, but I don't think that is what you're desiring. . .

    - - arden.

Re: Can Perl/CGI scripts respond to user selection?
by Grygonos (Chaplain) on Mar 10, 2004 at 21:47 UTC

    Ahh....Let me direct you to my first post in SoPW basically the same question you asked. While my question was a little different, in that the values of the second field (drop-down) would rely on a database query ran using the value selected in the first field, you are basically wanting the same thing I did... dynamic behavior w/o re-loading from a perl CGI form.

    Although perl cannot do it alone, TomDLux Is correct that Javascript will help you along the way.


    Grygonos
      Thank you for all your help. I got my scripts to work by using  -onChange=>'document.location..' and refreshing the page with a manually set query string. However, now, everytime the user selects a new pop-up menu, the page needs to be fetched afresh and more than the time to compute, it is the refresh time that makes the whole thing look really slow.

      The only way I can see of speeding it up is by using Javascript as adviced in the previous posts. Trouble, though, is, my data structures are fairly complex and after looking at the documentations for Data::JavaScript and Data::JavaScript::LiteObject, I could not quite a figure out how I could transfer data and how I could be using them in JavaScript. In fact, I thought, Data::JavaScript::Anon was probably more suited to my needs. But the documentation isn't very explantory and I could not find any good tutorials or examples. I am new to Javascript as well; if any of the monks could help me by providing pointers to documentation, tutorials, examples or just advice, I would be extremely grateful.

      Thanks,
      Ashish

Re: Can Perl/CGI scripts respond to user selection?
by TomDLux (Vicar) on Mar 10, 2004 at 21:37 UTC

    Your perl script can NOT do this, since the script runs before the page is displayed, and the user can select a menu option only after the page is displayed ... and therefore after the script has finished runnning.

    You can have the various lists for the second menu assigned to Javascript variables. Have a Javascript routine invoked when the first menu selection is made, to assign the appropriate values to the second menu.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA