2015-11-20 9 views
0

我有一個簡單的HTML表單是這樣的:獲取和textarea的回聲輸入多次

<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 
    
  • 鍵入ABO一些其他的事情五個文本區域並提交,結果顯示:

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

非常感謝!

+2

你不能使用javascript? – bassxzero

+0

如果您對javascript設置了死鎖,則可以使用隱藏輸入。這個問題是類似的http://stackoverflow.com/q/33331808/5051310 – Terminus

+0

謝謝@Terminus。我喜歡隱藏輸入的想法,但無法弄清楚,或者你認爲在我的情況下隱藏輸入可能不可能? – nogc

回答

1

如果你不想把這些值在數據庫中,你可以使用一個會話

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

不要在不清理post變量的情況下使用這段代碼,否則你會被黑客入侵。查看preg_replace()和php.net

+0

非常感謝你@Steve。它完美的工作,因爲我希望,但現在我試圖使用隱藏的輸入作爲Terminus上面提到的在我的問題的評論。你知道它是如何工作的嗎? – nogc

+0

您可能還想使用innerHTML--但您可能根本不想使用JavaScript,因此可能會脫離主題。類似於這個想法:http://stackoverflow.com/questions/33739624/dont-know-how-to-solve-between-php-and-javascript/33743744#33743744 – Steve

+0

我仍然無法弄清楚你的解決方案隱藏的輸入可能工作。我給了$ counter和$ texts一個起始值,並且每次提交$ counter和$ texts時都會重置爲起始值(0和「」)。也許你可以幫忙?非常感謝@Steve – nogc

2

如果你保持呼應了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,這是一個數組,並使其易於處理。