使用php记住表单条目

使用php记住表单条目,php,html,Php,Html,我有一张表格;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 /&

我有一张表格;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>

但这意味着我必须从
question.html
编写两次代码,一次用于
question.html
,另一次用于
question1.php
。有没有更省力的方法呢?

我建议您在一个PHP文件中构建所有内容。

将页面的数据发布到自身,并使用任何现有的
$\u 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>
完成!
请在下面再次提交

1) 问题1?


2) 问题2?
请注意,我的语法要求启用PHP的短标记。

如果未启用短标记,请替换
您是否可以使用
文件\u get\u contents
question1.php
中的类似内容加载
question.html
?我建议您在一个php文件中构建所有内容。将页面的数据发布到页面本身,并使用任何现有的$u Post值预先填充表单。我认为Session最适合这种类型的内容,并在最后将其全部写入..@showdev我该如何做?
<?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>