So we are working in a Programming class and the teacher is as lost as I am, I’m thinking maybe fresh eyes will spot the issue faster than we can. The idea is to create a word counter application and embed it into our page. I’ve got the general code down, but for some reason after counting the words it also displays the word “Count” under the results. So instead of just saying “Your text has __ words” it adds a "Count * " underneath that. Any help would be awesome cause like I said, even the teacher has told me she’s lost why it’s doing that, but I think it’s just tired eyes at this point.
here’s the full code:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Word Counter</title>
<link href="styles.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php
$text ="";
$texterror = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["words"])) {
$texterror = "More words are required.";
}
else{
$text = test_input($_POST["calc"]);
}
}
?>
<form method= "post" action= "<?php echo ($_SERVER["PHP_SELF"]);?>">
<textarea name ="words" cols = "60" row = "20" maxlength="50000" id = "words">
</textarea>
<br>
<input type ="submit" name= "calc" value ="Count"><span class ="error"> * <?php echo $texterror;?>
</span>
</form>
<?php
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if($text) {
$counted = str_word_count($_POST["words"]);
echo "<h2> Your text has ". $counted . " words. </h2>";
echo $text;
}
?>
</body>
</html>