重复获取并回显来自textarea的输入[关闭]
I have a simple HTML form like this:
<form action="index.php" method="post">
<textarea id="question" name="question"></textarea>
<input type="submit"/>
</form>
And the procedure that I'd like it to be is something like described here:
-
Type something in the text area and click submit, result displayed:
[... the only text area got blank and be ready for new input ...] Question 1: Something typed firstly
-
Type some other thing in the above text area and submit, result displayed:
[... the only text area got blank and be ready for new input ...] Question 1: Something typed firstly Question 2: Something typed secondly
-
And so on...
[... the only text area got blank and be ready for new input ...] Question 1: Something typed firstly Question 2: Something typed secondly ... Question n: Something typed at the n time
Does anyone have an idea or can give me a hint how to do this using only HTML and PHP?
Thank you very much!
我有一个简单的HTML表单,如下所示: p>
&lt ; form action =“index.php”method =“post”&gt;
&lt; textarea id =“question”name =“question”&gt;&lt; / textarea&gt;
&lt; input type =“submit”/&gt ;
&lt; / form&gt;
code> pre>
我想要的程序如下所述: p>
-
在文本区域输入内容,然后单击“提交”,显示结果: p>
[...唯一的文本区域为空白且为 准备好进行新输入......]
问题1:首先输入的内容
code> pre> li>
-
在上面的文本区域输入其他内容并提交 ,结果显示: p>
[...唯一的文本区域空白并准备好进行新输入...]
问题1:首先输入的内容
Question 2 :第二次输入
code> pre> li>
-
依此类推...... p>
[... 唯一的文本区域空白并准备好进行新输入...]
问题1:有些 首先输入的东西
问题2:其次输入的东西
...
问题n:n次输入的东西
code> pre> li>
ul>
非常感谢! p>
div>
If you don't want to put the values in a database you could use a session
session_start();
at the very top of your page and
<?php
if( $_SESSION['counter'] > 0 ) $_SESSION['texts'] = $_SESSION['texts'] . "<br />Question " . $_SESSION['counter'] . ": Something " . $_POST['question'];
$_SESSION['counter']++;
?>
<?php echo $_SESSION['texts']; // put this in a div where you want to see them ?>
This will only clear when the browser is shut.
This is a very crude outline but you could look at tutorials on sessions and how to use them.
You would also need a counter at the bottom of your script to add in the number.
$_SESSION['counter']++;
Using hidden inputs to replace sessions without array or for loop.
<?php
$counter = $counter + (int) $_POST['counter']; // (int) helps sanitise this by forcing type change to integer - any text would be converted to 0
//echo $counter; // for trackng
$texts = $_POST['texts']; // MUST be sanitized and escaped for mysqli
if($counter > 0) $texts = $texts . "<br />Question " . $counter . ": Something " . $_POST['question'];
$counter++;
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<textarea id="question" name="question"></textarea>
<input type="hidden" name="texts" id="texts" value="<?php echo $texts;?>" />
<input type="hidden" name="counter" id="counter" value="<?php echo $counter; ?>" />
<input type="submit"/>
</form>
<?php echo $texts; // put this in a div where you want to see them ?>
Do not use this code without cleaning up the post variables or you will get hacked to bits by hackers. See other posts on preg_replace()
and on php.net
You can use hidden inputs if you keep echoing out the id values and the just submitted value. (Also, note how I added a name to the submit)
<form action="index.php" method="post">
<?php
if(isset ($_POST['submit']) {
$oldAnswers = array ();
if(isset($_POST['oldAnswers']) && is_array ( $_POST['oldAnswers'])) {
$oldAnswers = $_POST ['oldAnswers'];
}
if (isset ($_POST ['question']))
$oldAnswers[] = $_POST ['question'];
for($i = 0, $j = 1; $i < count (oldAnswers); $i++, $j++) {
$answer = $oldAnswers [$i];
// display your "previously written" message
echo 'Question ' . $j . ': ' . $answer . "<br>
";
// echo out hidden inputs
echo '<input name="oldAnswers[]" type="hidden" value="' . $answer . '">';
}
}
?>
<textarea id="question" name="question">< /textarea>
<input name="submit" type="submit">
</form>
See how oldAnswers has []
after it in the name? That'll tell php that it's an array and make it easy to deal with.