[英]remember form entries using php
我有一个表格; url = question.html
:
<form class="text1" action="question1.php" method="post">
1) Question1?<br />
<textarea cols="80" rows="5" class="text" name="Answer1"></textarea>
<br /><br />
2) Question2?<br />
<textarea cols="80" rows="5" class="text" name="Answer2"></textarea>
</form>
然后将其提交给question1.php
,后者将帖子提交至txt
文件。 并打开一个新的HTML页done.html
在done.html
页面我希望能够回到question.html
,我想的是要记住里面的答案textarea
。 我目前通过使用php页面question1.php
再次编写页面来使其工作:
$answer1 = $_POST["Answer1"];
$answer2 = $_POST["Answer2"];
$fo = fopen("question.html", "w");
$write_this = '<form class="text1" action="question1.php" method="post">
1) Question1?<br />
<textarea cols="80" rows="5" class="text" name="Answer1">' . $answer1 . '</textarea>
<br /><br />
2) Question2?<br />
<textarea cols="80" rows="5" class="text" name="Answer2">' . $answer2 . '</textarea>
</form>'
fwrite($fo, $write_this);
fclose($fo);
但这意味着我必须从question.html
编写两次代码,一次是question.html
,另一次是question1.php
。 有没有那么省力的方法呢?
我建议您将所有内容都构建在一个PHP文件中。
将页面的数据发布到自身,并使用任何现有的$_POST
值预填充表单。
像这样:
<?php
// get posted data, or set to false if none exists
$answer1 = isset($_POST['Answer1'])?$_POST["Answer1"]:false;
$answer2 = isset($_POST['Answer2'])?$_POST["Answer2"]:false;
// if the form has been submitted, write to file and show "Done" message
if (!empty($_POST)) {
// write to file
$fo = fopen("question.html", "w")...... etc.
// display "Done" message
?><h1>Done!</h1>
<p>Submit again below.</p><?php
}
// display form, with any posted values included
// blank "action" attribute makes form submit to current page (same page)
?><form class="text1" action="" method="post">
1) Question1?<br />
<textarea cols="80" rows="5" class="text" name="Answer1"><?=$answer1?></textarea>
<br /><br />
2) Question2?<br />
<textarea cols="80" rows="5" class="text" name="Answer2"><?=$answer2?></textarea>
</form>
请注意,我的语法要求启用PHP的short标签。
如果未启用短标签,则将<?=
替换为<?php echo
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.