dt4320279 2015-11-20 00:11
浏览 68
已采纳

重复获取并回显来自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!

  • 写回答

2条回答 默认 最新

  • dqsk4643 2015-11-20 00:21
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码