记住使用php的表单条目

时间:2013-10-16 23:12:47

标签: 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 />
 <textarea cols="80" rows="5" class="text" name="Answer2"></textarea>
</form>

然后将其提交给question1.phptxt将帖子提交到done.html文件。并在done.html页面中打开一个新的html页面question.html,我希望能够返回textarea,并希望它能够记住question1.php内的答案。我目前通过使用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编写代码,再为{{1}}编写代码。这样做是否有一种不太费力的方式?

1 个答案:

答案 0 :(得分:1)

我建议您在一个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的短标记 如果未启用短标记,请将<?=替换为<?php echo