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

I was wondering if I can make a script which if a browser accesses it produces a form and which is also the target of the created form's action. I was thinking I could test to see if the form params are undefined and if they are produce the form and if they are defined, process them.
Feedback will be appreciated and used!
TIA
jg
_____________________________________________________
My name is Inigo Montoya...
  • Comment on Can the same script create a form and then process the form's contents on Submit?
  • Download Code

Replies are listed 'Best First'.
Re: Can the same script create a form and then process the form's contents on Submit?
by Masem (Monsignor) on Nov 15, 2001 at 19:56 UTC
    Certainly; this use of CGI is actually quite common. Probably the only thing you need to have is to include a HIDDEN form element that allows your script to determine what 'stage' it is in so that you can determine if it's the first visit or after they've pressed submit already. Typically, I do this by saying if this hidden element is undefined or doesn't match what I expect, I start with the blank forms; everything else is either redisplaying these or to present completion of tasks.

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    "I can see my house from here!"
    It's not what you know, but knowing how to find it if you don't know that's important

Re: Can the same script create a form and then process the form's contents on Submit?
by MZSanford (Curate) on Nov 15, 2001 at 19:58 UTC
    If you use CGI.pm, this is actually the default action of the <code lang="perl">start_form</code> function. I usually also add a hidden field, that by it's very existance make an easy if/else out of the "display form" and "process data" steps. The hidden field also makes it easy to add image creation by using "image" (in the case of createing Timelines,OrgCharts,GD, etc)
    i had a memory leak once, and it ruined my favorite shirt.
Re: Can the same script create a form and then process the form's contents on Submit?
by Rich36 (Chaplain) on Nov 15, 2001 at 20:17 UTC
    I haven't had a chance to try this, but it looks interesting. CGI::Application. What I gather is that it allows you to display multiple pages within the same application - which means you could have persistent variables throughout your program.
    Rich36
    There's more than one way to screw it up...

      I like the idea of combining CGI::Application with HTML::Template that they mentioned at the bottom. I wonder how CGI::Application does handling error pages. Slap it into Apache::Registry, and this could be slick indeed.
Re: Can the same script create a form and then process the form's contents on Submit?
by belg4mit (Prior) on Nov 15, 2001 at 20:38 UTC
    What I have found to be the easiest means of doing this is to give your submit button a name attribute like so:

    <input type=submit name=in value="Format HDD">

    And the browser will submit a in=Format%20HDD key-value pair to you. So upon loading you look for this key and value and if you have it you've been submitted. Pseduo-code follows.

    #use CGI or whatever; if( $param{in} eq 'Format HDD' ){ #process form submission #redirect elsewhere or print status screen } else{ #print form }

    -- perl -p -e "s/(?:\w);(<A HREF="/index.pl?node=st&lastnode_id=3333">st< +/A>)/'\$1/mg"