My instructor told us to make a PHP script that will count the vowels of whatever the user input in the textarea. At the same time show the result below it. And I already did it.
But then the instructor told us that she also wants to see at the same time after showing the result what the user input in the textarea.. I'm still searching how to do that.. Can someone help me with it?
I use PHP_SELF and method POST in <form>
tag to show the results.
<?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>
Output after clicking the count vowels button: The textarea should have user input text in it.
and will display the:
The sentence is $sentence. There are $countVowels($sentence) vowels in the sentence.
Why not do simply?
<?php
echo $message1;
echo "<br/>";
echo $message2;
echo "<br/>";
isset($_POST['display']) ? echo $_POST['sentence] : '';
?>