in reply to Basic CGI: 2 forms, 2 submits

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.

Replies are listed 'Best First'.
Re^2: Basic CGI: 2 forms, 2 submits
by chrism01 (Friar) on Feb 05, 2008 at 01:32 UTC
    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

    Chris

      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?)

      —Brad
      "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
        It's Perl & CGI::FormBuilder (as mentioned). Basically in script A you choose whether to create a new asset or update a known one, pass this info (using submit button) to new script (perl module) containing script B, which attempts to create or update asset as requested. Use B's submit button to insert/update DB.
        As standalone scripts they work, but I can't get it to work (as above) if I call B from A, which is the actual requirement.
        There's (will be) a form B for each asset type

        Anyway, here's the (awful) code. Sorry it's a bit long/messy, but I don't know enough to quickly make it any shorter.

        http://www.perlmonks.org/?viewmode=public;node_id=666195 my scratchpad