php - 报告生成器 - 保存并重定向到报告
问题描述
我正在创建一个报告生成器,它应该保存报告并将用户重定向到特定 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.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>
推荐阅读
- c++ - 混合功能是一种不好的做法吗
与其他课程? - elasticsearch - Searchkick 词干提取
- python - 具有非常高传播的插值
- angular - 如何使用snackbar处理Angular中的错误
- asp.net-mvc - 无法在控制器中获取 webApi 结果
- java - 如何使对象有效地沿一个方向移动?
- deep-learning - 应用于傅里叶变换信号的卷积滤波器
- typescript - 值与其键匹配的对象的 TypeScript 类型
- go - 将 Gorm 查询映射到结构列表
- ansible - Ansible:有没有办法查看文件,根据特定标准拆分文件中的内容,然后将该内容复制到另一个文件?