Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Basic CGI: 2 forms, 2 submits

by chrism01 (Friar)
on Feb 05, 2008 at 01:03 UTC ( #666166=perlquestion: print w/replies, xml ) Need Help??
chrism01 has asked for the wisdom of the Perl Monks concerning the following question:

This is a basic CGI qn I'm sure, but I don't know how to make it work.

I've got 2 forms, A & B, both of which work fine standalone (and both have a submit button). What I need to do is call B from A, passing a couple of params.
If I do this by adding B's code to the file containing A's code, or putting B in a perl module (my pref as there will be about 25 different form Bs to call) & clicking A's submit, then form B comes up fine and (in update mode) fills the fields from the DB.
However, ctrl seems to pass back to the calling form A in the background, such that when I click the submit button on form B (which is displayed), it actions the form A submit button, so the form B is not actioned(!).
I also tried using system() or exec() instead, but they just fail with 'unexpected end of headers' and don't even load B.

FYI, I'm using CGI::FormBuilder, but I believe this is a generic CGI qn; how do you use multiple forms, each with their own submit (pref separate code files for each)?


Replies are listed 'Best First'.
Re: Basic CGI: 2 forms, 2 submits
by dsheroh (Prior) on Feb 05, 2008 at 01:17 UTC
    That sounds like an HTML issue to me rather than anything to do with Perl/CGI. More specifically, that sounds a lot like what happened to me the last time I forgot that HTML does not allow forms to be nested.

    Have you double-checked the combined page to verify that the two sets of <form>...</form> tags don't overlap?

Re: Basic CGI: 2 forms, 2 submits
by olus (Curate) on Feb 05, 2008 at 01:17 UTC

    From your description it seems that the 'action' on both forms is the same target script, and since you want to use the same script to do different actions, you can use hidden form fields that help the script determine the step it is at. Inside the script use conditionals that check the value of the hidden step field and act accordingly.

      Actually, they are both separate scripts, and work standalone as I mentioned. Basically, in script A I input an action ie create or update, and for update I input an item id as well eg item-001. I then pass both params to script B, which gets the related info from the DB and displays it for possible alteration. It also displays another submit button.
      The problem is that although script/form B is now displayed, it seems that the code has already returned to A, because when I click submit in B, it actually re-submits A, therefore B's changes are lost.
      I think it's something to do with CGI being a stateless protocol, but I'm not a CGI prog, I do back end stuff, so I don't know how to tell it to submit the displayed form ie B.

      Hope that's clearer


        If you have two submit buttons on the same form you will need to check which one was clicked
        use CGI; my $q = CGI->new; my $bA = $q->param('button_A'); my $bB = $q->param('button_update'); if($bA eq "whatever") { # do whatever } if($bB eq "Update") { # do update stuff here }

        chrism01, how about some code snippets? I spend all day in CGI forms and you've lost me with your prose explanation. Nothing like a little code to illustrate what you are doing.

        I am curious, though: 1) how are you displaying/generating the HTML? and 2) how are you populating and then outputing B? (is that done in your Perl? or through some templating method?)

        "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
Re: Basic CGI: 2 forms, 2 submits
by naikonta (Curate) on Feb 05, 2008 at 07:06 UTC
    Perhaps, you need CGI::Application? It works very well in conjunction with CGI::Form Builder.

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://666166]
Approved by olus
What's the matter? Cat got your tongue?...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2017-08-18 14:55 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (303 votes). Check out past polls.