【问题标题】:How to POST to another page without user interaction?如何在没有用户交互的情况下发布到另一个页面?
【发布时间】:2013-11-25 01:30:09
【问题描述】:

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

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

【问题讨论】:

  • 你不知道 - PHP 中唯一可行的方法是标头请求,它只能执行 GET 参数。最简单的方法是输出一个您自动提交的表单
  • 一种选择是使用会话并将会话名称分配给您希望分配给的变量。 编辑“如何在没有用户点击提交按钮的情况下在 php 中做到这一点?” 正如@Pekka웃所说的那样。
  • 让 JavaScript 为您按下按钮。 (只是不是真的;form.submit())。
  • 如果其他页面在我们的控制之下,会话是最好的答案;如果它是一个外国页面,他们不会为你做太多。

标签: php html post


【解决方案1】:

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

例如:

<?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>

【讨论】:

  • 那应该是value="&lt;? echo $post_username; ?&gt;" 缺少echo;
  • 已编辑。感谢您的关注:)
  • 哦,这是假设 OP 的短标签为“ON”。否则,它需要是 value="&lt;?php echo $post_username; ?&gt;" 很多人不知道这一点,所以如果你愿意,可以将其作为脚注指出。
【解决方案2】:
    $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);

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

【讨论】:

  • 但这会导致 服务器 发出 POST 请求,而不是客户端。这通常是一场完全不同的球赛
【解决方案3】:
$.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,您可以在两者之间发布参数。希望对您有所帮助!

【讨论】:

    【解决方案4】:

    一个有用的方法是使用 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
    

    【讨论】:

    • 这只是从其他页面获取数据,并没有真正将用户发送到其他网页。
    【解决方案5】:

    Amadan 正在做某事。

    只是把这个 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>
    

    【讨论】:

    • 为了记录,我给自己打了勾而不是 aiman9000,因为我在提交答案时击败了他,这是我最终使用的实际代码格式。
    猜你喜欢
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    • 1970-01-01
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    • 2012-05-04
    • 2010-10-10
    相关资源
    最近更新 更多