【问题标题】:How to carry POST variables to another file?如何将 POST 变量传送到另一个文件?
【发布时间】:2015-12-11 06:57:16
【问题描述】:

我正在处理一个表单,并希望保存提交的每个表单的副本。问题是表单的操作是一个计数文件,它使每个保存的文件计数一次。例如,提交的第三个表单命名为“3.php”,提交的第十个表单命名为“10.php”。当我尝试将 POST 变量写入新文件的顶部时,它就消失了。无论如何,我可以用我的计数文件代码写他们对新文档的回复吗?

主文件上的表单代码:

<form action="count.php" method="post">
    <input type="text" name="formItem1">
    <input type="text" name="formItem2" required>
    <input type="text" name="formItem4" required>
    <input type="text" name="formItem5" required>
    <input type="text" name="formItem6" required>
    <input type="submit" name="Click" value="Submit">
</form>

Count.php 代码:

<body>
<?php
define('countlog.txt', __DIR__ . "./");

if (file_exists('countlog.txt')) {
    # If File exists - read its content
    $start = (int) file_get_contents('countlog.txt');
} else {
    # If No File Found - Create new File
    $start =  1;
    @file_put_contents('countlog.txt', "$start");
}

# When Form Submitted
if (isset($_POST) and isset($_POST['Click']) and $_POST['Click'] == "Submit") {
    $file_name = "{$start}.php";
    $template = file_get_contents('template.php');
    @file_put_contents("./submissions/" . $file_name, "$template");
    # Update Counter too
    $start = $start + 1;
    @file_put_contents('countlog.txt', "$start", 1);
    echo "Generated Filename - $file_name";
}
?>
</body>

Template.php 代码:

echo "<h1>Answer1: " . $formItem1 . "</h1>";
echo "<h1>Answer2: " . $formItem2 . "</h1>";
echo "<h1>Answer3: " . $formItem3 . "</h1>";
echo "<h1>Answer4: " . $formItem4 . "</h1>";
echo "<h1>Answer5: " . $formItem5 . "</h1>";
echo "<h1>Answer: " . $formItem6 . "</h1>";

【问题讨论】:

  • 我看不到你在哪里引用$_POST['formItem1']?你在某处使用extract($_POST) 吗?顺便说一句,这似乎是一种非常复杂的实现非常简单的方法?如果您的目标是将提交的数据存储到文件中,难道您不能只对发布的数据进行 json 编码并将其保存在单个文本文件中的新行上,而不是弄乱不同的文件和计数日志吗?
  • @Tristan 好吧,我想让文件数起来,这很好。但是提交表单时会保存帖子值,当我尝试将它们写入新生成的文件时,它们不存在。我也尝试将帖子值设为常规变量,但在尝试存储它们时出现错误。也许一种存储帖子值并在新文件中调用它们的方法会有所帮助?
  • @Tristan 另外,我有count log的原因是为了保存每个表单提交,count log给每个提交一个唯一的文件名。
  • 这一行$template = file_get_contents('template.php');template.php 的内容变成了一个字符串。如果您想评估它以用发布的值替换变量,您需要使用 eval() php.net/manual/en/function.eval.php

标签: php html forms post


【解决方案1】:

使用会话。使用session_start(); 在每个页面上创建一个会话,使用会话全局数组存储帖子值。例如。 $_SESSION['yourValue'] = POST['yourValue'];。现在在其余页面上,您应该能够访问该值。例如

$yourValue = $_SESSION['yourValue'];

【讨论】:

  • 我会将这个添加到哪个文件中?
  • 添加 $yourValue = SESSION['yourValue'];到新文件。
  • "注意:使用未定义的常量 SESSION - 假定为 'SESSION'" 在我放置您的代码的那一行。我不需要在某个地方定义这个值吗?
  • 您是否使用 session_start() 开始会话;我编辑了我的答案。我最初犯了一些语法错误。
  • 谢谢!我也喜欢简短的回答!
【解决方案2】:

如果您使用$template = file_get_contents('template.php');,它将使template.php 的内容成为字符串,并且不会计算变量。

您可以使用 eval() http://php.net/manual/en/function.eval.php 将字符串评估为 PHP 代码。我不推荐这种方法,但如果你想使用它,你需要return,而不是echo模板,否则模板将回显到浏览器而不是你要保存到文件的字符串。

模板.php

return "<h1>Answer1: " . $formItem1 . "</h1>".
"<h1>Answer2: " . $formItem2 . "</h1>".
"<h1>Answer3: " . $formItem3 . "</h1>".
"<h1>Answer4: " . $formItem4 . "</h1>".
"<h1>Answer5: " . $formItem5 . "</h1>".
"<h1>Answer: " . $formItem6 . "</h1>";

count.php

$template = file_get_contents('template.php');
$template = eval($template);

include 模板会更容易/更好,代码将执行,从而填充 $template 变量。

模板.php

<?php
$template = "<h1>Answer1: " . $formItem1 . "</h1>".
"<h1>Answer2: " . $formItem2 . "</h1>".
"<h1>Answer3: " . $formItem3 . "</h1>".
"<h1>Answer4: " . $formItem4 . "</h1>".
"<h1>Answer5: " . $formItem5 . "</h1>".
"<h1>Answer: " . $formItem6 . "</h1>";
?>

count.php

include('template.php');

这两种方法都假定您已使用extract($_POST); 将变量从数组导入到当前符号表中。

【讨论】:

  • 谢谢,但在看到您的帖子之前,我已经让另一个工作了。用他的方法,我还得把写好的数据缩小
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-02
  • 2021-05-25
  • 1970-01-01
  • 2020-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多