Click to See Complete Forum and Search --> : Submitting to 2 CGI scripts with one form


yertle
03-14-2003, 03:50 PM
I am currently using a CGI script that came free with my host, and it is not open to be modified. I have another script that I would like to use along with the existing script, which is basically just going to keep a log of everything so I can keep some statistics of how many people use the form.

My question is, is there any way to run both of these scripts with the single form? I need information from the form to be submitted to both scripts. I tried doing <form method="post" action="1st.cgi" action="2nd.cgi"> and some variations on that, but it didn't work. Is there any way to nest 2 forms, or anything like that? Or can I make a third CGI script that will submit the information to the other two?

Thanks for the help,
-Brian Nehring

Alhazred
03-15-2003, 10:25 AM
Well, not really... See a form post is really just getting a page from the web server, so 'submitting to 2 CGIs' would be like clicking on one link and getting 2 pages back at once, there just isn't any technique for that really.

That being said, there ARE ways to get what you want. The best way would be to write your own script. Given that you have 2 scripts that do things you want in theory if they are well written it shouldn't be too tough to cut and paste them together into something that gives you the features you need.

Unfortunately this obviously means learning Perl, and if they are crappy scripts like 90% of the ones floating around on the web you may well end up just writing your own.

yertle
03-17-2003, 02:48 PM
The problem is actually that the first script is hosted by my ISP, and is not accessible to be edited. Is there any way to call scripts from another script? I could make a third script, which would call the 2 that I currently have, and give the same information to them both.

-Brian Nehring

smoker2
04-11-2003, 01:40 PM
Why not use javascript in your form page to add the users entries to a series of hidden form fields in a separate form.
Then leave the first form action to post to the server supplied script, and use an OnClick event for the main form button to submit the second form to your own script.

Make your script return 1;
so that it can finish executing without displaying a page.

alan

Alhazred
04-11-2003, 02:08 PM
Originally posted by smoker2
Why not use javascript in your form page to add the users entries to a series of hidden form fields in a separate form.
Then leave the first form action to post to the server supplied script, and use an OnClick event for the main form button to submit the second form to your own script.

Make your script return 1;
so that it can finish executing without displaying a page.

alan

Because when you submit the first form, your page is GONE, the page you now have is the result of the submission of the 1st form. There simply isn't any practical way to submit from one page to two forms... There are MAYBE some ways you could create such a situation, if the 1st script returned an HTTP status of 'no response' (I forget the status code and I am not going to look it up) which simply leaves your browser at the same page it was on already, but this STILL requires modifying the 1st script. Even so I've never tested doing this and I am not sure what state it leaves the browser in, it may behave as if the page was just reloaded...

Overall I still say the best thing is to write a script. You could base it on the one the ISP uses.

It IS possible for perl to submit a form to a URL, so in principle there is nothing stopping you from building a 3rd CGI which acts as an HTTP client and submits to 2 other scripts. I'd consider it kind of kludgy, but its doable. Check out LWP::Simple, that module gives you all the functionality you most likely would need to accomplish this.

smoker2
04-12-2003, 05:10 AM
Because when you submit the first form, your page is GONE, the page you now have is the result of the submission of the 1st form. There simply isn't any practical way to submit from one page to two forms...

This is not relevant, the page that has the forms on, is only one page. It contains 2 forms, one hidden one not. Javascript can make them both contain the same data, and also using onClick for the submit button would allow the posting of the hidden form to your own script.

The browser will get the 1st scripts output as its next page, you make your 2nd script return nothing to the broswer, so there are no conflicts.

I will do a demo later.

Alternatively, why can't you send the data to your script first, then forward the same data from your script to the original script and let that script return the page.

alan

Alhazred
04-12-2003, 07:48 AM
You will have to show me then, because what you are proposing is NOT POSSIBLE. Either that or I totally misunderstand what you mean. Maybe explaining it step by step will clarify.

1) You have a page with 2 forms on it, one is hidden, via javascript it has the same contents as the 1st form.

2) You submit the 1st form.

3) The web server returns results for the 1st form, WIPING OUT THE 2ND FORM.

4) There is no 4...

As I pointed out above, you MIGHT be able to get away with something like this scenario with the 1st script returning an HTTP 'no content' result, but browsers are not particularly consistent about what they do next because there actually IS a result and some versions of browsers will simply treat the existing page as having been reloaded, which is likely to cause you problems.

Now, another alternative is to return a result from the 1st form which is a page/javascript that immediately initiates a post to script 2.

However my understanding was that script 1 cannot be changed because it belongs to the ISP. If he could get around that then he could just write his own script and the issue is moot.

Nedals
04-12-2003, 03:34 PM
I remembered seeing this a while back.. It may be what you are looking for.

http://forums.webdeveloper.com/showthread.php?s=&threadid=3481

I don't know how you pass the variables, but JeffMott may be able to help there.

smoker2
04-13-2003, 12:25 PM
Hi

Ok, maybe I spoke too soon. After hacking around, I find that unless the forms are submitted separately with a small time delay, the second script doesn't receive any data.
It does do the work but the time delay is constantly changing which makes it impractical for use. Strangely, if I use the back button after submitting to the first script, then the second script kicks in !

I just thought, if the original poster has access to put his own scripts on the server, then why bother using the server supplied script at all ?

Secondly, how about using his own script to record the form input, which then returns a page containing a form whose action is set to the server supplied script.

It could be done as a "Please confirm your details" type page.
The fields could be hidden so that they wouldn't be aware of the difference in destination.

just a thought

alan

DaiWelsh
04-15-2003, 07:24 AM
Some more suggestions along similar lines:-

1. Form submits to your script, your script processes then redirects browser to third party script with required data. This is tricky if second script depends on data coming HTTP POST (would need javascript I think).

2. Form submits to your script which processes then calls third party script with required data. Again this is harder if second script depends on data coming HTTP POST, but not much harder. Perl, PHP, Cold Fusion etc. all have ways to call another page/script from within a script.

3. Javascript method suggested above, but set target of form to be a hidden frame to get past the problem raised about the page being lost after first submit. Disadvantage is js dependence.

4. Similar to above but have the whole duplicate form in the hidden frame to start with.

HTH,

Dai

Nedals
04-15-2003, 12:31 PM
I just thought, if the original poster has access to put his own scripts on the server, then why bother using the server supplied script at all ?smoker2 makes a very good observation!