LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Basic CGI: 2 forms, 2 submits: HOW? (https://www.linuxquestions.org/questions/programming-9/basic-cgi-2-forms-2-submits-how-618708/)

chrism01 02-04-2008 07:04 PM

Basic CGI: 2 forms, 2 submits: HOW?
 
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)?

shakezilla 02-04-2008 08:56 PM

At the risk of a n00b questioning someone of 2000 someodd posts, why exactly are you doing this? Is it because you're POSTing to 2 different servers? If that's the case, couldn't the CGI script receive info from one form and POST to another server from within itself?

chrism01 02-04-2008 10:25 PM

Actually, I'm new to CGI myself ... that's why I'm stuck ;)
No, script/form A allows you to choose an item type and either create a new one or update a known one.
If a known one, also enter the item id eg itemA-001.
Then call form B from A (by clicking A's submit button).
There will be one script/form 'B' for each item type. Create or update item as requested. Click submit button on form B to insert/update DB.
As separate / standalone scripts/forms, they work fine. Calling B from A, B appears, but the prog context seems to rtn to A after displaying B, so although I click on B's submit, I actually get A's action.
I believe it's something to do with CGI being a stateless protocol, but I don't know how to deal with that.

shakezilla 02-04-2008 11:22 PM

Well I don't think I can help you with the 2 forms issue, because I've never dealt with anything like that. All the cgi I've ever done has been from the assumption of a single cgi request having either one GET, one POST, or both, but never more than one of each.

The only advice I can give you is to work on your process flow so your app fits into the above model. Correct me if I'm wrong, but it sounds like you want to process info from A, then form B is dependent on that, then after clicking SUBMIT on B, all the relevant data (from both A and B) is sent to the server. If this is your case, then the hidden input type may be useful to you. The hidden type will allow you to capture values from A and "store" them temporarily in B.

Code:

<form>
        <input type="hidden" name="itemId">A-001_weGotThisFromA</input>
        <input type="text" name="itemQty"></input>
</form>

The above method can be used when you have steps in your process which are dependent on previous steps. When your CGI on the server is writing form B, have it include within B the info from A. Then the POST from B will send what you need.

Hope that helps.


EDIT
----
Just thought of one more thing. With the above method, you may actually be able to use 2 <form>'s on a single page, just form A won't have an action. Then use some pretty slick JS to auto fill in the hidden inputs, resulting in 1 server request instead of 2. But warning, that JS will be complicated, don't mess with that unless it's justified by a need for high volume.

chrism01 02-04-2008 11:33 PM

Passing data from A to B is no problem.
The problem is that once I've called B eg (pseudo code, conceptually)
Code:

(in A)

get_field_vals();
call_B($arg1, $arg2);        # by clicking A's 'submit' button


(in b)

collect_params();
get_data_from_DB();
display_data_for_update();
update_db();            # by clicking B's 'submit' button

prob is that the code rtns to A as soon as it's displayed the form, so when I click B's button (on screen), the code in A catches the click.

shakezilla 02-05-2008 12:00 AM

Ok, I've misunderstood your problem. I'm out of my league on this one, so hopefully someone else here can lend a hand.

chrism01 02-05-2008 12:12 AM

"I'm out of my league on this one" you and me both :)
Thanks for trying anyway


All times are GMT -5. The time now is 04:38 AM.