Multiple Form Actions?

Mike

New member
Jun 27, 2006
6,777
116
0
51
On the firing line
I need to submit some data to two different places, one to a flat file on Site A and another time to a database on Site B. Both sites accept HTTP posts, but I can't figure out how to have two actions in the form.

Identical data, one submit button push, two different URLs.

Any ideas?
 


I don't think you can do random javascript requests to random domains, and your form can only have one destination. What about a cronjob that regulary pushes updates from A->B or B->A?
 
What about using one page to push the data to the other?

So, when the post hits Site A it automatically pushes the data to Site B?

Is that possible?
 
If one of the sites (say Site A) is on the same domain as the form that you are submitting from, then you can use AJAX to submit to Site A and then have the actual form submit action post to Site B. Bind a function to the onsubmit method of the form and have that make the XMLRequest to Site A and then have that submit the form when it completes. jquery will make all this pretty easy if you've ever used it before.
 
If one of the sites (say Site A) is on the same domain as the form that you are submitting from, then you can use AJAX to submit to Site A and then have the actual form submit action post to Site B. Bind a function to the onsubmit method of the form and have that make the XMLRequest to Site A and then have that submit the form when it completes. jquery will make all this pretty easy if you've ever used it before.

Shit, that sounds like exactly what I need, but I only understand about 0.005% of how to implement it. Fuck. Okay, I'm off to Google to learn jquery, XMLRequest and binding functions or something.
 
Dude, just post to your own PHP script that uses cURL to re-post to both of the destinations.
 
[edit]Just re-read it, you can do it easily with jQuery. Grab the values of all the form elements, and use 2 ajax request. I don't have time to write an example right now, but if it isn't solved by time I get home (roughly another 5 hours) I'll whip an example up.
 
If you do the cURL repost, it's worth noting that the referrer on all those posts is going to be yourscript.php -- I'm willing to go out on a limb and guess that SiteB doesn't know you're doing this, and if it's an advertiser, and you're leaching emails from them, there's like a 50-50 chance they'll bitch at your aff network to shut you down. Asking permission first can go a long way in this situation*.

*I don't normally advocate asking permission.
 
Luckily, this is for a client and not an aff network, and they are fully aware of what I'm doing for them (or trying), so referrers are not an issue.

Anyhow, I tried just duplicating this:

Code:
$Curl_Session = curl_init('http://SiteA.tld/page1.php');
curl_setopt ($Curl_Session, CURLOPT_POST, 1);
curl_setopt ($Curl_Session, CURLOPT_POSTFIELDS, "blah=$blah&etc=$etc");
curl_setopt ($Curl_Session, CURLOPT_FOLLOWLOCATION, 1);
curl_exec ($Curl_Session);
curl_close ($Curl_Session);
and changed the url to page2.php. Then I copied page1 and put it up on page2. Well, it works and posts to both places, but it displays both pages on the curl.php page. How can I surpress the display of the second page?


/edit/ coding outside of my basic skillset always makes me feel like a retard. ugh.
 
Here's the jist of what you need to know:

Code:
//gets the value of an element with the id 'element'
var val1 = $('#element').val();
//gets the value of an element with the class element
var val2 = $('.element').val();

//for get use $.get
$.post("test1.php", { var1: val1, var2: val2 } );
$.post("test2.php", { var1: val1, var2: val2 } );
 
Here's the jist of what you need to know:

Code:
//gets the value of an element with the id 'element'
var val1 = $('#element').val();
//gets the value of an element with the class element
var val2 = $('.element').val();

//for get use $.get
$.post("test1.php", { var1: val1, var2: val2 } );
$.post("test2.php", { var1: val1, var2: val2 } );

And that's jquery? Okay, thanks! I'll go look up how to use it now! :D
 
Luckily, this is for a client and not an aff network, and they are fully aware of what I'm doing for them (or trying), so referrers are not an issue.

Anyhow, I tried just duplicating this:

Code:
$Curl_Session = curl_init('http://SiteA.tld/page1.php');
curl_setopt ($Curl_Session, CURLOPT_POST, 1);
curl_setopt ($Curl_Session, CURLOPT_POSTFIELDS, "blah=$blah&etc=$etc");
curl_setopt ($Curl_Session, CURLOPT_FOLLOWLOCATION, 1);
curl_exec ($Curl_Session);
curl_close ($Curl_Session);
and changed the url to page2.php. Then I copied page1 and put it up on page2. Well, it works and posts to both places, but it displays both pages on the curl.php page. How can I surpress the display of the second page?


/edit/ coding outside of my basic skillset always makes me feel like a retard. ugh.


curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
 
Anyhow, I tried just duplicating this:
Code:
$Curl_Session = curl_init('http://SiteA.tld/page1.php');
curl_setopt ($Curl_Session, CURLOPT_POST, 1);
curl_setopt ($Curl_Session, CURLOPT_POSTFIELDS, "blah=$blah&etc=$etc");
curl_setopt ($Curl_Session, CURLOPT_FOLLOWLOCATION, 1);
curl_exec ($Curl_Session);
curl_close ($Curl_Session);
and changed the url to page2.php. Then I copied page1 and put it up on page2. Well, it works and posts to both places, but it displays both pages on the curl.php page. How can I surpress the display of the second page?

Code:
curl_setopt($Curl_Session, CURLOPT_RETURNTRANSFER, TRUE);

Yes, ^^^ set that option and also do something like:

$blah = curl_exec($Curl_Session);

Then the output is in that variable instead of on the screen. Now you'll have to output something for the user though. (Why this cURL way is cool is because your script can interpret the response from both POSTs and then do a variety of things depending on what happened. Or you can just do echo $blah after whichever post you actually want to display the output from.)
 
oh, wait, I think I see what I can do...

curl_setopt ($Curl_Session, CURLOPT_FOLLOWLOCATION, 1);

for the second post should be set to "0" and then it won't output to the screen, right?
 
Yes, ^^^ set that option and also do something like:

$blah = curl_exec($Curl_Session);

Then the output is in that variable instead of on the screen. Now you'll have to output something for the user though. (Why this cURL way is cool is because your script can interpret the response from both POSTs and then do a variety of things depending on what happened. Or you can just do echo $blah after whichever post you actually want to display the output from.

Okay, so if I read this right, by using the CURLOPT_RETURNTRANSFER I could, in theory, use it to verify that the other script / page received the data? Like send myself an email with the http response? 200 or 404 or whatever?

cURL is badass! Confusing, but badass nonetheless.

nope, that just determines whether or not it'll follow a 301 redirect.

AH! Okay, thanks Uplinked!
 
Yea, curl itself won't output to the page. I just wrap curl up into a file_get_the_contents() function (check the warchest). To get it to output you'd have to echo it out somehow.

For your issue, just do a curl at the top of submit1.php that gets/posts the variables elsewhere then continue the output page however you would if were only a single form submit.
 
Okay, so I finally found out exactly where the client needs the data sent to, and they do provide a server response. I just pasted the URL into the address bar, hit go and got "Failure". So, how to capture that?

Code:
$Curl_Session = curl_init('http://SiteA.tld/page1.php');
curl_setopt ($Curl_Session, CURLOPT_POST, 1);
curl_setopt ($Curl_Session, CURLOPT_POSTFIELDS, "blah=$blah&etc=$etc");
curl_setopt ($Curl_Session, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($Curl_Session, CURLOPT_RETURNTRANSFER, TRUE);
$blah = curl_exec($Curl_Session);
mail("myemailaddress@email.com", $subject, $blah, $from);
curl_close ($Curl_Session);
Would that work to email me the response?

/edit/ found out the answer is "Yes". A slight mod to the above code and it emailed me the response I was looking for. Yay! Finally, I can move on to the next project and put this client to bed.

Thanks for all the help guys! I love you (in a very hetero way) :D
 
Last edited: