Send a POST request across multiple pages
Rayfi
Posted messages
234
Status
Member
-
Xavierdu34 Posted messages 223 Status Member -
Xavierdu34 Posted messages 223 Status Member -
I have a personal project in PHP, actually I want to try to send a single POST request to multiple pages. I'll give you a diagram
My page that will send the POST request = MPRP
Target that will receive the request = CMP (there are several)
MPRP ===========> CMP, CMP2, CMP3, CMP4, etc...
If you understand correctly, I just want to send the same POST request to multiple pages at the same time, and without redirection.
I don't know if it's a form or code in PHP.
My page that will send the POST request = MPRP
Target that will receive the request = CMP (there are several)
MPRP ===========> CMP, CMP2, CMP3, CMP4, etc...
If you understand correctly, I just want to send the same POST request to multiple pages at the same time, and without redirection.
I don't know if it's a form or code in PHP.
8 answers
-
I know nothing about PHP, but it's not possible with a post:
the best thing would be to make a post to a page that will handle the distribution ;-)
I'm saying this without any knowledge, so don't hit me x)
--
------------------------------------------------------------------------------------
"Fear leads to anger. Anger leads to hate. And hate ... leads to suffering." - Yoda -
Hello,
go take a look here, if I understood correctly, this is what you're trying to do:
https://forum.tomsguide.fr/threads/formulaire-php-deux-pages.307942/
--
Ride the Best or Die like the Rest-
-
Hello,
Since a request (POST or GET) = 1 destination, I only see 3 possibilities.
1 - Either you go for "distributed" like sugel said earlier, that is to say
MPRP
|
CMP
/ | \
CMP2, CMP3, CMP4, etc...
and it's not complicated with a minimum of PHP knowledge
2 - or you go for "series" MPRP -> CMP1 -> CMP2 -> CMP3 ....
(but I don't really like it personally)
3 - or you go for "parallel," and I think that's a bit what you want with constraints, that is to say, make as many POST requests as there are CMP (and it's doable in JavaScript)
Normally we have 1 form, 1 submit so 1 POST request, but we can have
1 form, 1 button that launches a JavaScript that makes several POST requests (in Ajax for example)
there may be other solutions
Best regards
-
-
Although I don't see the point of sending to multiple PHP pages, you could perform several PHP processes on the same page. If it's to display the result in your browser, only one page will be shown.
Otherwise, if you really insist, a possible solution is in JavaScript/Ajax.
(You might want to look into jQuery Ajax.)
This would involve sending multiple HTTP requests simultaneously to CM1, CM2, etc., at the time the form is posted.
However, it seems that the CM scripts need to be on the same domain. -
In fact, I give up, I prefer to try with JavaScript which should be simpler. The only problem is how to receive a POST request with JavaScript via an HTML form?
I need to go through PHP, but I need a code or a link. -
Hello,
Here is a simple example of sending a form to multiple PHP files using jQuery.
<form id="myForm" action="" method="POST">
<label>Last Name: <input type="text" name="last_name" /></label>
<label>First Name: <input type="text" name="first_name" /></label>
<input type="submit" id="send" value="Send" />
</form>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
// when the form is submitted
$('#myForm').on('submit', function() {
// serialize the form data to send it
var data = $('#myForm').serialize();
// send the form information to the different addresses
$.post('http://www.example.com/file1.php', data);
$.post('http://www.example.com/file2.php', data);
$.post('http://www.example.com/file3.php', data);
// ...
return false; // prevent the browser from submitting the form itself
});
});
</script>
Have a nice day. -
In addition, here is an equivalent in PHP:
<?php
// when the form is submitted
if (isset($_REQUEST['send'])) {
// we build our URL parameters
$data = http_build_query($_REQUEST);
// we send the form information to different files
file_get_contents('http://www.example.com/file1.php?' . $data);
file_get_contents('http://www.example.com/file1.php?' . $data);
file_get_contents('http://www.example.com/file1.php?' . $data);
}
?>
<form id="myForm" action="#" method="POST">
<label>Name: <input type="text" name="name" /></label>
<label>First Name: <input type="text" name="firstname" /></label>
<input type="submit" name="send" value="Send" />
</form>
Happy coding! -
It is possible in PHP thanks to Curl :
<?php $ch = curl_init(); curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); curl_setopt($ch, CURLOPT_URL,"envoi.php"); // target page curl_setopt($ch, CURLOPT_POST, 1); // Using the POST method curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST); // The array to POST curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // To get the return of the page in a variable, see $output later. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, '5');// the time before a connection timeout. curl_setopt($ch, CURLOPT_REFERER, 'localhost'); // referring url curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // allows following header redirections. $output = curl_exec($ch); // executes and returns the result in $output; curl_setopt($ch, CURLOPT_URL,"envoi2.php"); // target page 2 $output = curl_exec($ch); curl_close ($ch); ?>
Of course, the PHP Curl extension must be enabled. (available on most hosts by default or to be added in a php.ini)-
An article to help you understand the use of cURL: http://ww12.xrecasens.info
-
-
And why not simply use sessions? That's what they're for, right?
In your form, you change your target to verify.php, in verify.php you put:
session_start(); if (!isset($_POST['post1']) OR !isset ($_POST['post2'])) { echo 'Please fill in all fields'; } else { $_SESSION['post1'] = $_POST['post1']; $_SESSION['post2'] = $_POST['post2']; }
Then, on all your other pages, you retrieve the posts under the variable$_SESSION['post...']
not forgetting to always include the session_start();session_start();
before loading the HTML page.
--
TiDi