Входы и эхо-сигналы от textarea неоднократно

У меня есть простая 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']++; 

Использование скрытых входов для замены сеансов без массива или цикла.

  <?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 ?> 

Не используйте этот код, не очищая почтовые переменные, или вы будете взломаны хаками хакерами. Посмотреть другие сообщения на preg_replace() и на php.net

Вы можете использовать скрытые входы, если вы повторяете значения id и только что переданное значение. (Также обратите внимание, как я добавил имя в 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>\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, что это массив и упрощает работу с ним.