首页 > 解决方案 > 报告生成器 - 保存并重定向到报告

问题描述

我正在创建一个报告生成器,它应该保存报告并将用户重定向到特定 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>

标签: phpredirectphp-7.3

解决方案


如果你放https://se.yahoo.com的是action表单将表单数据发布到的属性,那么它根本不会进入这个脚本

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

推荐阅读