in reply to Equivalent functionality in CGI.pm

Why on earth are you trying to get CGI.pm to produce javascript, much less html? CGI.pm is horribly suited for producing html in the first place, adding javascript is going to get very ugly. Switch to decent templating system for the love of god.

Replies are listed 'Best First'.
Re^2: Equivalent functionality in CGI.pm
by Your Mother (Archbishop) on Jul 04, 2004 at 19:23 UTC

    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.

      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.

Re^2: Equivalent functionality in CGI.pm
by MSchienle (Novice) on Jul 04, 2004 at 19:46 UTC
    The JavaScript isn't being produced by CGI, it's just being called by the CGI script. It lives in its own separate file and gets called in with a -script=> line. I just included it on the chance that I had written the JavaScript wrong and that the CGI could be rewritten to work correctly if the JavaScript could be rewritten. Or something like that.
    Mike Schienle