0

I have two files.

a HTML form like so:

<form action="resubmitWithPHP.php" method="POST">
  <label for="fname">First name:</label>
  <input type="text" id="fname" name="fname"><br><br>
  <label for="lname">Last name:</label>
  <input type="text" id="lname" name="lname"><br><br>
  <input type="submit" name=submitButton value="Submit">
</form>

where user inputs thier details and after clicking submit get transfered to 'resubmitWithPHP.php'

where this script is waiting is:

<?php 
if(isset($_POST['submitButton']))
{
    $firstName = $_POST['fname'];
    $lastName = $_POST['lname'];
?>
      <form action="otherPage.php" method="POST">
      <label for="fname">First name:</label>
      <input type="hidden" id="fname" name="fname" value="<?php echo $firstName; ?>"><br><br>
      <label for="lname">Last name:</label>
      <input type="hidden" id="lname" name="lname" value="<?php echo $lastName; ?>"><br><br>
      <input type="submit" name=submitButton value="Submit">
    </form>
    <?php
}
else
{
    header("Location: ../goBack.php?sent=nope");
}
?>

My question is, how do I get this new form to submit without any further interaction from the user? I want the page to automatically submit the form as soon as it is possible to do so. Preferably without loading or displaying anything.

babis95
  • 582
  • 6
  • 29
  • Its a strange usecase.. why you need to submit it again? .. You just could pass the variables as GET `(?fname=&lname=)` using `header();` but its insecure. – ChrisG Jun 08 '20 at 21:36
  • This really feels like you are trying to do something the hard way... – Chris Haas Jun 08 '20 at 21:36

1 Answers1

0

You dont need a form or submit for that, you can do this with php by sending a POST request. Check this out: How do I send a POST request with PHP?, answer by dbau

So what you want will probably be something like this.

<?php 
if(isset($_POST['submitButton']))
{
    $firstName = $_POST['fname'];
    $lastName = $_POST['lname'];

    $url = "http://localhost/otherPage.php";
    $data = ['fname' => $firstName, 'lname' => $lastName];
    $options = array(
        'http' => array(
            'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
            'method'  => 'POST',
            'content' => http_build_query($data)
        )
    );
    $context  = stream_context_create($options);
    $result = file_get_contents($url, false, $context);


}
else
{
    header("Location: ../goBack.php?sent=nope");
}
?>

Hope this helps, best regards

milanC64
  • 131
  • 5