andrewpo
andrewpo

Reputation: 371

Send $_POST to 2 php files at once

i have no idea how to solve a problem with sending $_POST. I want to fill a form at example.com

//at example.com
<form action="foo.php" method="post" >
<input name="bar1" type="text" />
<input name="bar2" type="text" />
<input name="bar3" type="text" />
<input value="Send" type="submit" />
</form>

and then it goes to foo.php :

<?php //foo.php
echo 'added: <p>'.$_POST['bar1'].'<br />'.$_POST['bar2'].'<br />'.$_POST['bar3']; 
?>

and in the same time it also send

$_POST['bar1'], $_POST['bar2'], $_POST['bar3']

to exampledomain.com/foobar.php where it can be saved to a file - that's not a problem.

I don't know how to send info to both php scripts at once - one is external one. I guess i have to send it somehow inside foo.php

There is kind of solution - redirecting to exampledomain.com/foobar.php inside foo.php but it isn't acceptable in my case - I want to do it without making user exit example.com

Thanks in advance and hope you can undestand my problem - if not just ask a comment

EDIT: Based on Pete Herbert Penito's answer:

 <?php //inside foo.php
 $url = 'http://exampledomain.com/foobar.php';
 $fields_string='';
 foreach($_POST as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
 rtrim($fields_string,'&');

 //open connection
 $ch = curl_init();

 //set the url, number of POST vars, POST data
 curl_setopt($ch,CURLOPT_URL,$url);
 curl_setopt($ch,CURLOPT_POST,count($_POST));
 curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

 //execute post
 $result = curl_exec($ch);

 //close connection
 curl_close($ch);

  ?>

Upvotes: 0

Views: 456

Answers (3)

trapper
trapper

Reputation: 12003

You will need to do one of the POST's with javascript ajax.

Then the real post which will redirect the browser like normal.

http://www.w3schools.com/jquery/ajax_post.asp

$(selector).post(url,data,success(response,status,xhr),dataType)

Upvotes: 0

Doug Molineux
Doug Molineux

Reputation: 12431

I would use CURL to construct a post request:

<?php
 // these variables would need to be changed to be your variables 
 // alternatively you could send the entire post constructed using a foreach
 if(isset($_POST['Name']))     $Name   = $_POST['Name'];
 if(isset($_POST['Email']))   $Email   = $_POST['Email'];
 if(isset($_POST['Message']))   $Message= htmlentities($_POST['Message']);

 $Curl_Session = curl_init('http://www.site.com/cgi-bin/waiting.php');
 curl_setopt ($Curl_Session, CURLOPT_POST, 1);
 curl_setopt ($Curl_Session, CURLOPT_POSTFIELDS, "Name=$Name&Email=$Email&Message=$Message");
 curl_setopt ($Curl_Session, CURLOPT_FOLLOWLOCATION, 1);
 curl_exec ($Curl_Session);
 curl_close ($Curl_Session);
?>

From Link:

http://www.askapache.com/php/sending-post-form-data-php-curl.html

Upvotes: 2

flowfree
flowfree

Reputation: 16462

In your foo.php:

<?php
include 'http://exampledomain.com/foobar.php';

Note: you need to enable allow_url_fopen in your php.ini file.

Upvotes: 1

Related Questions