【发布时间】: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