如何在没有用户交互的情况下POST到另一个页面


How to POST to another page without user interaction?

因此,我遇到这样一种情况:用户通过表单提交一些数据,然后单击提交按钮,该按钮指向一个单独的.php页面,在该页面上进行处理。处理完成后,我需要转到另一个.php页面,并随它一起发送一个我已经知道其值的POST变量。

在html中,我会制作一个带有输入和提交按钮的表单。如何在php中做到这一点而不让用户单击提交按钮?

我能想到的最简单的方法是将上一页的输入放入具有隐藏输入类型的表单中。

例如:

<?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>
    $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);

代码取自这里,另一个问题可能会为您提供一些有用的答案。

$.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
  });

如果使用Json或Xml,则可以在两者之间发布参数。希望它能有所帮助!

一个有用的方法是使用CURL方法。

$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

阿玛丹发现了什么。

刚刚粘贴了这个HTML添加到我的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>