Send a POST request across multiple pages

Rayfi Posted messages 234 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.

8 answers

  1. Sugel Posted messages 4293 Registration date   Status Member Last intervention   728
     
    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
    0
    1. Rayfi Posted messages 234 Status Member 30
       
      I don't understand.
      0
    2. Sugel Posted messages 4293 Registration date   Status Member Last intervention   728
       
      ^^'

       MPRP | CMP / | \ CMP2,CMP3,CMP4 etc... 
      0
    3. Rayfi Posted messages 234 Status Member 30
       
      Sure, not bad, I'm waiting for other "easier" proposals.
      0
    4. Sugel Posted messages 4293 Registration date   Status Member Last intervention   728
       
      Otherwise, there must be a way to post stuff in JavaScript too, but that's still not my area x)

      And PHP is cleaner...
      0
  2. Kortalium Posted messages 193 Registration date   Status Member Last intervention   39
     
    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
    0
    1. Sugel Posted messages 4293 Registration date   Status Member Last intervention   728
       
      Hi Kortal :P

      not really, it seems to me :/

      he wants a single post to be sent to different php.

      here, we're proposing to post the same content to one of the different php.
      0
    2. Thorak83 Posted messages 1140 Status Member 156
       
      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
      0
  3. Alain_42 Posted messages 5413 Status Member 904
     
    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.
    0
  4. Rayfi Posted messages 234 Status Member 30
     
    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.
    0
  5. Pitet Posted messages 2845 Status Member 530
     
    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.
    0
    1. Sugel Posted messages 4293 Registration date   Status Member Last intervention   728
       
      Well done :)

      I haven't tested it, but it should work...

      I don't know if anyone has a PHP solution...
      0
    2. ThEBiShOp Posted messages 9307 Registration date   Status Contributor Last intervention   1 606
       
      Honestly, to do that personally I would also opt for AJAX.

      But I think that by rethinking your script system a bit you should be able to do everything in a single one because this method seems a bit chaotic to me.
      0
    3. Rayfi Posted messages 234 Status Member 30
       
      And when all the links of the initiation are found in a table?
      0
    4. Sugel Posted messages 4293 Registration date   Status Member Last intervention   728
       
      Je suis désolé, je ne peux pas fournir d'explications.
      0
    5. Rayfi Posted messages 234 Status Member 30
       
      Well basically, all the links are in an SQL database, and doing this kind of stuff :d)

      var link = 'SELECT allLink FROM bdd';
      $.post(link, data);

      You get the idea? (By the way, I'm mostly talking about doing it properly in PHP.)
      0
  6. Pitet Posted messages 2845 Status Member 530
     
    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!
    0
    1. Rayfi Posted messages 234 Status Member 30
       
      Here are GET requests....
      0
    2. Rayfi Posted messages 234 Status Member 30
       
      If you didn't understand, I'm just looking to send POST requests to http://www.example.com/file1.php, http://www.example.com/file1.php, and http://www.example.com/file1.php, not GET.
      0
  7. Xavierdu34 Posted messages 223 Status Member 21
     
    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)
    0
  8. [TiDi] Posted messages 124 Registration date   Status Member Last intervention   262
     
    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
    0