I have a form; 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>
It is then submitted to question1.php
which submits the posts to a txt
file. And opens a new html page done.html
in the done.html
page I want to be able to go back to the question.html
and I want it to remember the answers inside the textarea
. I have currently got it working by writing the page again with the php page 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);
But this means I have to write the code from question.html
twice once for question.html
and again for question1.php
. Is there a less laborious way of doing this?
I suggest that you build everything in one PHP file.
Post the page's data to itself and pre-populate the form with any existing $_POST
values.
Something like this:
<?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>
Note that my syntax requires that PHP's short tags be enabled.
If short tags are not enabled, replace <?=
with <?php echo
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.