in reply to Re: Equivalent functionality in CGI.pm
in thread Equivalent functionality in CGI.pm

I agree. And I'm not saying you should do otherwise but I end up putting JS into CGIs now and then anyway (throwaways, portability, timeframe, etc).

One way to do it when you feel the need to is with HERE docs. You can inline them too.

print start_html( -title => $title, -script => <<HEREisAscript, <!-- function focus_first_field(){document.forms[0].elements[0].focus();} // --> HEREisAscript -onload => 'focus_first_field()', ), <<HERESanother, <script type="text/javascript"> <!-- function toggleEmail(evt) { // longer script omitted... } // --> </script> HERESanother 'Show Email:', radio_group(-id => 'hideEmail', -name => 'radioEmail', -values => [ qw(Yes No) ], -default => 'No', -onclick => 'toggleEmail(event)' );

There is more information about using JS this way in the CGI perldoc itself.

Replies are listed 'Best First'.
Re^3: Equivalent functionality in CGI.pm
by MSchienle (Novice) on Jul 04, 2004 at 20:08 UTC
    The radio_group code you provided is pretty close to what I started out with, I just couldn't get it to work with the JavaScript. And I couldn't get CGI to spit out a single radio_group using both the hideEmail and showEmail ID's. I think my real problem is sticking with CGI beyond it's usefulness. I've been checking out templating systems for a couple months, but I'm adding to an application that's been around for a lot longer. Any new development will likely be using TT.

    What I've done in the last few minutes is this, which works correctly, but it just seems wrong to make two calls to radio_group, each with one button. That's due to the JavaScript needing two different id's as currenly written.
    'Show Email:', $q->radio_group( -id => 'hideEmail', -name => 'radioEmail', -values => 'No', -default => 'No', -onclick => 'toggleEmail(event)' ), $q->radio_group( -id => 'showEmail', -name => 'radioEmail', -values => 'Yes', -default => 'No', -onclick => 'toggleEmail(event)' ),

    Mike Schienle

      Yeah, sorry, I missed the different IDs. Two calls is fine. You could also change the JS to operate off a "this" instead of an "event" and get down to one CGI call. The JS functions would have to do different checking of state and such though.

        I think I'll save the JS changes for another day. I'm knee deep into Spreadsheet::WriteExcel at the moment. I don't think I can handle another context switch for a while.

        Thanks for the pointers.
        Mike Schienle