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

Working with the CPAN module CGI – within a single form, can I have multiple SUBMIT buttons that POST data off to different pages? I want something like:

Color: <input type="radio" name='color' value="Red" /> Red <input type="radio" name='color' value="Blue" /> Blue
Action: <input type="submit" name='paint' value="Paint"/> <input type="submit" name='crayon' value="Crayon"/>

I want the Paint button to POST the color to paint.cgi and the Crayon button to POST the color to crayon.cgi.

But the 'action' of the CGI::form call determines where the SUBMIT buttons lead to. Can I have multiple destinations within a single form? If I go to two forms in the page, can they somehow both have access to the 'color' param?

throop

  • Comment on POSTing off to different pages in CGI module

Replies are listed 'Best First'.
Re: POSTing off to different pages in CGI module
by merlyn (Sage) on Sep 11, 2007 at 15:39 UTC
    Not strictly within HTML. You can use Javascript to do what you want, but then anyone with Javascript stripped or turned off or untrusted for your site will get the one single action. Maybe you just want separate smaller forms around your individual buttons, or handle it server-side with a redirect to the right form instead?
      Thanks, ww and merlyn,

      Clearly, I need to read up on redirection. The CGI.pm documentation gives me some basics. But I need to understand how GET/POST data gets passed, and which nph and status codes to use.

      So far, I haven't found it in Super Search or any of my O'Reily books. Any pointers?

      again, thanks
      throop

Re: POSTing off to different pages in CGI module
by ww (Archbishop) on Sep 11, 2007 at 15:36 UTC
    Short answer: no. The scope of a submit is the form of which it's a part.

    But the short answer is misleading.

    There's nothing to prevent you from parsing the input to distinguish by a field in the form (hidden if you wish) that determines whether the script calls crayon.cgi or paint.cgi. That doesn't mean you have to double-deck the scripts:

    form.html calls firstscript.pl
    firstscript.pl parses the field and calls one or the other

    as you can incorporate the selection/dispatch in subs inside firstscript.pl

Re: POSTing off to different pages in CGI module
by naikonta (Curate) on Sep 12, 2007 at 01:38 UTC
    I never have this situation since I believe that each application should only have exactly one .cgi file acting as gateway for the browsers. The rest of the business processes are implemented in modules. In the server side, decisions are executed by method calling, not URL dispatching. Playing with HTTP specs somewhat PITA for me. But I can understand if you run into such situation by inheriting some legacy codes.

    BTW, thanks for providing information on the POST redirecting thing (++). As the page you refer to says, I'm one of them that don't realize the fact. OTOH, I rarely use redirection to respond a request. Upon reading the page, I'm glad that I don't rely on POST redirecting. I think I would make some modifications on the application structure instead of trying to provide a single form that can be handled by more than one CGI program. It just doesn't make sense to me.


    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!