簡體   English   中英

在PHP的同一頁面上顯示其結果時,如何顯示用戶在文本字段中鍵入的內容?

[英]How can I show what the user types in the text field while I display its result on the same page in PHP?

我的老師告訴我們制作一個PHP腳本,該腳本將計算用戶在文本區域中輸入的任何內容的元音。 同時在下面顯示結果。 而且我已經做到了。

但是,然后講師告訴我們,她還希望在顯示結果后同時查看用戶在文本區域中輸入的內容 。.我仍在尋找操作方法。.有人可以幫助我嗎?

我在<form>標記中使用PHP_SELF和POST方法來顯示結果。

<?php
error_reporting(0);
$text = $_POST['sentence'];

if ($_POST['display']){
    $message1 = "The sentence is \"$text\".";
    $message2 = "There are ".countVowels($text)." vowels in the sentence.";
}
 function isVowel($ch){
     $flag = false;
     $vowels = "aeiou";
     for ($i = 0; $i < strlen($vowels); $i++)
        if ($ch == $vowels[$i])
            $flag = true;
    return $flag;
 }
 function countVowels($str){
     $counter = 0;
     for($i = 0; $i < strlen($str); $i++)
        if (isVowel($str[$i]) == true)
            $counter++;
    return $counter;
 }
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Count the vowels</title>
    </head>
    <body>
        <form action = "<?php echo $_SERVER['PHP_SELF'];?>" method = "POST">
            <p>Enter a sentence: </p>
            <textarea name ="sentence" cols = "75" rows = "20" placeholder = "Type here..." ></textarea>
            <br/>
            <input type = "submit" name = "display" value = "Count Vowels"/>
            <?php
                echo $message1;
                echo "<br/>";
                echo $message2;
            ?>
        </form>
    </body>
</html>

單擊“計數元音”按鈕后的輸出:文本區域中應包含用戶輸入的文本。

並顯示:

這句話是$句子。 句子中有$ countVowels($ sentence)個元音。

為什么不簡單地做呢?

<?php
     echo $message1;
     echo "<br/>";
     echo $message2;
     echo "<br/>";
     isset($_POST['display']) ? echo $_POST['sentence] : '';
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM