从textarea重复获取和回应输入 [英] Get and echo inputs from textarea repeatedly
问题描述
我有一个简单的HTML表单,如下所示:
< form action =index.phpmethod =post >
< input type =submit/>
< / form>
我希望它的过程如下所述:
-
在文本区域输入内容并点击提交,结果显示:
<$ p $[...唯一的文本区域空白并准备好新的输入...]
问题1:首先输入的内容
$ c
$ b $在上面的文本区域输入一些其他的东西并提交,结果显示:
[...唯一的文本区域变为空白并准备好接受新的输入...]
问题1:首先输入的内容$ b $问题2:其他类型输入
-
等等...
[...唯一的文本区域空白并准备好新输入...]
问题1:首先输入的东西是
问题2:输入的东西其次是
...
问题n:在n次输入的东西
有没有人拥有一个想法或可以给我一个提示如何使用HTML和PHP做到这一点?
非常感谢!
如果您不想将值放入数据库中,您可以使用会话
session_start();
位于页面顶部并且
<?php
if($ _SESSION ['counter']> 0)$ _SESSION ['texts'] = $ _SESSION ['texts']。 < br />问题。 $ _SESSION ['counter']。 :什么。 $ _ POST [问题];
$ _SESSION ['counter'] ++;
?>
<?php echo $ _SESSION ['texts']; //把它放在你想看到它们的div中?>
这只会在浏览器关闭时清除。
这是一个非常粗略的概述,但您可以查看会话教程以及如何使用它们。
您还需要在您的底部有一个计数器脚本添加数字。
$ _SESSION ['counter'] ++;
使用隐藏输入来替换没有数组或循环的会话。
<?php
$ counter = $ counter +(int)$ _POST ['counter']; //(int)通过强制将类型更改为整数来清除此内容 - 任何文本都将转换为0
// echo $ counter; // for trackng
$ texts = $ _POST ['texts']; //如果($ counter> 0)$ texts = $ texts,则必须进行消毒和转义以获得mysqli
。 < br />问题。 $柜台。 :什么。 $ _ POST [问题];
$ counter ++;
?>
< form action =<?php echo $ _SERVER ['PHP_SELF'];?>方法= POST >
< input type =hiddenname =textsid =textsvalue =<?php echo $ texts;?> />
< input type =hiddenname =counterid =countervalue =<?php echo $ counter;?> />
< input type =submit/>
< / form>
<?php echo $ texts; //把它放在你想看到它们的div中?>
不要在不清理post变量的情况下使用这段代码,否则你会被黑客攻击。查看 preg_replace()
和php.net上的其他帖子
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!
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
这篇关于从textarea重复获取和回应输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!