报告生成器 - 保存并重定向到报告

Report generator - save and redirect to report

我正在创建一个报告生成器,它应该保存报告并将用户重定向到特定 url 的报告版本(在本例中我只是添加了 yahoo 作为测试)。

问题:是否可以在同一脚本中保存文件并将用户重定向到 url?

问题:脚本执行重定向但不存储文件。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Report generator</title>
</head>
<body>

<pre>

  <form method="post" action="https://se.yahoo.com" target="_blank">

    <label for="fname">First name:</label><br>
    <input type="text" id="fname" name="fname"><br>
    <input type="submit" name="button_1" value="button_1">

  </form>

  <?php

  var_dump($_POST);

    // Button event listener.

    if (isset($_POST["button_1"])) {

      $json_data = "string";
      $data = json_encode($json_data);

      file_put_contents(
        "user_data/data.txt",
        $json_data
      );

    }

  ?>

</body>
</html>

如果您输入 https://se.yahoo.comaction 属性,表单将 post 表单数据发送到该属性,所以它根本不会转到此脚本

<?php

// Button event listener.
if (isset($_POST["button_1"])) {

    $json_data = "string";
    $data = json_encode([$json_data]);

    file_put_contents("user_data/data.txt",$json_data);
    // really not sure if this is what you wanted to do, bit of a guess really
    header('Location: https://se.yahoo.com?fname='.$_POST['fname']);
    exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Report generator</title>
</head>
<body>

  <form method="post" action="" target="_blank">

    <label for="fname">First name:</label><br>
    <input type="text" id="fname" name="fname"><br>
    <input type="submit" name="button_1" value="button_1">

  </form>
  </body>
</html>