0

我有一个表格;网址= 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文件。done.html并在done.html我希望能够返回的页面中打开一个新的 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. 有没有更省力的方法来做到这一点?

4

1 回答 1

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.

于 2013-10-16T23:23:54.743 回答