0

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!

nhle
  • 57
  • 9
  • 2
    You can't use javascript? – bassxzero Nov 20 '15 at 00:12
  • You can use hidden inputs if you're dead set against javascript. This question was similar http://stackoverflow.com/q/33331808/5051310 –  Nov 20 '15 at 00:59
  • Thanks @Terminus. I like the idea of hidden input but can't figure it out OR do you think might it be impossible with hidden input in my case? – nhle Nov 20 '15 at 03:22
  • @nogc - have just posted a new version - please let me know if that is what you wanted – Steve Dec 03 '15 at 16:18

2 Answers2

2

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

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.

1

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

Steve
  • 808
  • 1
  • 9
  • 14
  • Thank you very much @Steve. It works perfectly as I wish but now I'm trying to use hidden input as Terminus mentioned above in the comment on my question. Do you have any idea how it might work? – nhle Nov 20 '15 at 03:25
  • You might also want to play with innerHTML - but you may not want JavaScript at all so might be off topic. Similar to this idea: http://stackoverflow.com/questions/33739624/dont-know-how-to-solve-between-php-and-javascript/33743744#33743744 – Steve Nov 20 '15 at 12:30
  • I still can't figure out how your solution with hidden input might work. I gave $counter and $texts a starting value at the top and every time after submitting $counter and $texts keep being reset to those starting values (0 and ""). Maybe you can help? Thanks very much @Steve – nhle Dec 03 '15 at 14:27
  • @nogc - take the bottom version just edited - it submits to itself at present - not sure exactly from where/to where you wanted it to go but on the same page it will run as per the basic specification above. – Steve Dec 03 '15 at 15:19
  • @nogc - make sure the form is after all the code or it won't receive the submitted values - that is probably why it wasn't working. I usually do all my php and deliver the results as little php tagged "slugs" to html - sometimes you may want variables with html and php variable values so use single inverted commas to hold the html so you can keep proper double ones for that, then close the single inverted commas where you want your variable and put a dot, then the variable than another dot and open the next single inverted comma for more html until you close the last one and add a semi-colon. – Steve Dec 03 '15 at 16:15
  • @nogc $counter and $texts don't need any starting value as they are produces as the page gets submitted. If you set them then all you will get is what you set. – Steve Dec 06 '15 at 12:37
  • Thank you very much @Steve. I've just figured it out after some debugging. And thanks for your tips with inverted commas, it was very helpful :) – nhle Dec 07 '15 at 09:35
  • Well done - Terminus' method is still a good one too. You should post your updated version for others to reference. You can add html to it with classes to the texts to help you style them e.g.`$texts . "
    Question " . $counter . ": Something " . $_POST['question' . !];` and you can add divs in the same way - remember the spaces as in `: Something`
    – Steve Dec 07 '15 at 11:39