0

So I have a situation where a user submits some data through forms, then clicks a submit button which directs to a separate .php page where processing is done. After the processing is done I need to go to another .php page and send along with it a POST variable I already know the value of.

In html I would make a form with input(s) and a submit button. How do you do that in php without having a user click a submit button ?

user1086516
  • 887
  • 3
  • 9
  • 21
  • 2
    You don't - the only feasible way in PHP is a header request, which can do only GET paramteres. The easiest way to go is to output a form that you auto-submit – Pekka Nov 25 '13 at 01:32
  • One option is to use sessions and assign a session name to the variable you wish to assign it to. **EDIT**: *"How do you do that in php without having a user click a submit button?"* As per what @Pekka웃 said. – Funk Forty Niner Nov 25 '13 at 01:32
  • Make JavaScript push the button for you. (only not really; `form.submit()`). – Amadan Nov 25 '13 at 01:32
  • Sessions are the best answer if the other page is under our control; if it's a foreign page, they don't do much for you. – Amadan Nov 25 '13 at 01:33

5 Answers5

2

The simplest way I can think of is to put the input from the previous page in a form with hidden input type.

For example:

<?php
$post_username = $_POST['username'];
?>

<form id="form1" action="page2.php" method="post">
<input type="hidden" id="hidden_username" value="<?php echo $post_username; ?>" />
</form>

<script>
document.getElementById("form1").submit();
</script>
aiman9000
  • 55
  • 1
  • 1
  • 7
0
    $url = 'http://server.com/path';
$data = array('key1' => 'value1', 'key2' => 'value2');

// use key 'http' even if you send the request to https://...
$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);

var_dump($result);

Code taken from here, another question which may provide you with some useful answers.

Community
  • 1
  • 1
Alex
  • 4,674
  • 5
  • 38
  • 59
  • But this will result in the *server* making a POST request, not the client. That's a whole different ball game usually – Pekka Nov 25 '13 at 01:32
0
$.ajax({
  type: "POST",
  url: "YOUR PHP",
  data: { PARAMS }
}).done(function( msg ) {
    if(SUCCESS)
    {
$.ajax({
  type: "POST",
  url: "ANOTHER PAGE",
  data: { PARAM }
})
  .done(function( msg ) {
//Process Here
  });

You can post arguments in between if you use Json or Xml. Hope it helps !

Abraham K
  • 624
  • 1
  • 8
  • 24
0

A useful way is to use the CURL method.

$url = "test.php";
$post_data = array(
    "data1"=>$value1,
    ....
);
$ch = curl_init();

curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
//we are doing a POST request
curl_setopt($ch,CURLOPT_POST,1);
//adding the post variables to the request
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);

$output = curl_exec($ch);
curl_close($ch);

echo $output;//or do something else with the output
wangXL
  • 66
  • 2
0

Amadan was on to something.

Just stuck this HTML add the end of my php:

<html>
<form id="form" action="webAddressYouWantToRedirectTo.php" method="POST">
<input type="hidden" name="expectedPOSTVarNameOnTheOtherPage" value="<?php echo $varYouMadePreviouslyInProcessing ?>">
</form>
<script>
document.getElementById("form").submit();
</script>
</html>
user1086516
  • 887
  • 3
  • 9
  • 21
  • Just for the record, I gave myself the checkmark instead of aiman9000 because I beat him to the punch in submitting an answer and this is the actual code format I ended up using. – user1086516 Dec 02 '13 at 02:36