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

Hello Monks,


I am attempting to create a website using Perl-CGI.

I would like to create dynamic forms, i.e. click on a radio button and the form input changes. (something not unlike this (click on the single/married/divorced radio button))

My question is this:
Since I am using a templating system (HTML::Template) I am able to embed JavaScript in the template which will achieve the desired results, but is there a better 'Perly' way to do this? I use a handler of sorts to detect when a page is changed, i.e.:

#!/usr/bin/perl use strict; use warnings; use CGI; my $q = new CGI; my %States = ( Default => \&default_page, Home => \&home_page, ); my $current_screen = $q->param(".State") || "Default"; die "No Screen for $current_screen" unless $States{$current_screen}; while (my ($screen_name, $function) = each %States){ $function->($screen_name eq $current_screen); }

The problem with this, is it requires a full reload and re rendering of the page... Any thoughts? Thanks in advance

Replies are listed 'Best First'.
Re: Dynamic forms In Perl
by CountZero (Bishop) on May 14, 2011 at 21:57 UTC
    Have a look at CGI::Ajax.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Dynamic forms In Perl
by ambrus (Abbot) on May 14, 2011 at 21:47 UTC

    From the example you link to, I believe you can afford for the user to download all the variations of the form, you only want to hide some parts unless some option is chosen. Is this right? The alternative is when the potential additions to the form are so varied or impossible to compute on client side that you need to use a query to the server to get the new elements that have to be inserted.

    If so, I have a related example at Re: replicating blocks of code in perl and cgi. This one replicates form elements instead of hiding them, but the basic idea should be the same.

      > I believe you can afford for the user to download all the variations of the form, you only want to hide some parts unless some option is chosen.

      If you disable JS you will the that thats almost what the example does.

      All variations are visible, activated JS is just hiding all variable parts onLoad().

      Cheers Rolf

Re: Dynamic forms In Perl
by Anonymous Monk on May 14, 2011 at 21:00 UTC
    The problem with this, is it requires a full reload and re rendering of the page... Any thoughts?

    That is a problem with your javascript, the thing responsible for hiding the full reload.

Re: Dynamic forms In Perl
by Anonymous Monk on May 15, 2011 at 01:10 UTC
    Probably the most common way to handle user input these days is to use some kind of client-side JavaScript library (take your pick), and AJAX. For whatever driver you are likely to use, there are ready-made Perl interfaces for the server side. "The program that the user deals with" is a Javascript program, not Perl.
Re: Dynamic forms In Perl
by LanX (Saint) on May 15, 2011 at 17:47 UTC
    I did something like thisą in the past, but the real problem was to have all options functional also for non-JS users!

    For the JS-part and template toolkit:

    I had all possible HTML-chunks created on the server side and included as hidden templates into the text.

    The buttons just inserted them per JS onClick().

    I don't know of any CPAN-module helping you there.

    Cheers Rolf

    1) actually it was more comnplex, because the number of potential combinations was infinite.