反复获取和回显来自文本区域的输入
Get and echo inputs from textarea repeatedly
我有一个像这样的简单 HTML 表格:
<form action="index.php" method="post">
<textarea id="question" name="question"></textarea>
<input type="submit"/>
</form>
我希望的程序类似于此处描述的内容:
在文本区域输入内容并点击提交,结果显示:
[... the only text area got blank and be ready for new input ...]
Question 1: Something typed firstly
在上面的文本区域输入其他内容并提交,结果显示:
[... the only text area got blank and be ready for new input ...]
Question 1: Something typed firstly
Question 2: Something typed secondly
等等...
[... 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
有没有人有想法或可以给我提示如何仅使用 HTML 和 PHP 来做到这一点?
非常感谢!
如果您不想将值放入数据库中,您可以使用会话
session_start();
在页面的最顶部
<?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 ?>
这只有在浏览器关闭时才会清除。
这是一个非常粗略的大纲,但您可以查看有关会话以及如何使用它们的教程。
您还需要在脚本底部添加一个计数器来添加数字。
$_SESSION['counter']++;
使用隐藏输入替换没有数组或 for 循环的会话。
<?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 ?>
请勿在未清理 post 变量的情况下使用此代码,否则您将被黑客攻击。在 preg_replace()
和 php.net
上查看其他 post
如果您不断回显 id 值和刚刚提交的值,则可以使用隐藏输入。 (另外,请注意我是如何在提交中添加名称的)
<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>\n";
// echo out hidden inputs
echo '<input name="oldAnswers[]" type="hidden" value="' . $answer . '">';
}
}
?>
<textarea id="question" name="question">< /textarea>
<input name="submit" type="submit">
</form>
看看 oldAnswers 的名字后面有多少 []
?这将告诉 php 它是一个数组并且易于处理。
我有一个像这样的简单 HTML 表格:
<form action="index.php" method="post">
<textarea id="question" name="question"></textarea>
<input type="submit"/>
</form>
我希望的程序类似于此处描述的内容:
在文本区域输入内容并点击提交,结果显示:
[... the only text area got blank and be ready for new input ...] Question 1: Something typed firstly
在上面的文本区域输入其他内容并提交,结果显示:
[... the only text area got blank and be ready for new input ...] Question 1: Something typed firstly Question 2: Something typed secondly
等等...
[... 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
有没有人有想法或可以给我提示如何仅使用 HTML 和 PHP 来做到这一点?
非常感谢!
如果您不想将值放入数据库中,您可以使用会话
session_start();
在页面的最顶部
<?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 ?>
这只有在浏览器关闭时才会清除。
这是一个非常粗略的大纲,但您可以查看有关会话以及如何使用它们的教程。
您还需要在脚本底部添加一个计数器来添加数字。
$_SESSION['counter']++;
使用隐藏输入替换没有数组或 for 循环的会话。
<?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 ?>
请勿在未清理 post 变量的情况下使用此代码,否则您将被黑客攻击。在 preg_replace()
和 php.net
如果您不断回显 id 值和刚刚提交的值,则可以使用隐藏输入。 (另外,请注意我是如何在提交中添加名称的)
<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>\n";
// echo out hidden inputs
echo '<input name="oldAnswers[]" type="hidden" value="' . $answer . '">';
}
}
?>
<textarea id="question" name="question">< /textarea>
<input name="submit" type="submit">
</form>
看看 oldAnswers 的名字后面有多少 []
?这将告诉 php 它是一个数组并且易于处理。