0

I made a program that lets 6 grades be entered into a form, and then calculates the letter grade and the average. The only problem I'm having is that I can't figure out how to get the form to accept decimals. I tried changing my filter_input statement to sanitize floats, but that doesn't seem to fix the problem. Any thoughts would be appreciated.

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1">
 <title>Grades</title>
 <link rel="stylesheet" href="bootstrap.min.css">
 <script>
     window.onload = () => {
         document.getElementById('quiz1').focus();
         document.getElementById('quiz1').select();
     }
 </script>
 <style>
     input[type="text"] {width: 4em !important; text-align: right; margin-left: .5em; }
     ul { list-style-type: none; padding-left: 0; }
 </style>
</head>
<body>
<div class="container">
 <div class="col-lg-6 mx-auto text-center">
     <h1>Enter Quiz Grades</h1>
     <form action="." method="post">
         <?php
         // declare an array to store bowling scores
         $quizScores = array();
         // declare a variable to be used for the natural counting of the number of games
         $numberOfScores = 6;
         // declare a variable to be used for array elements - subtract 1 since array elements start at 0
         $gamesArrayElements = $numberOfScores - 1;

         for ($i = 0; $i <= $gamesArrayElements; $i++){
             $quizNumber = $i + 1;
             // load values from POST into array elements
             if (strlen($_POST['quiz' . $quizNumber]) > 0){
                 $quizScores[$i] = filter_input(INPUT_POST, 'quiz' . $quizNumber, FILTER_SANITIZE_NUMBER_FLOAT);
             }
             echo "<div class='form-group form-inline justify-content-center'>\n";
             echo "<label for='quiz$quizNumber'>Quiz $quizNumber: </label>\n";
             echo "<input type='text' class='form-control' name='quiz$quizNumber' id='quiz$quizNumber' value='$quizScores[$i]'>%\n";
             echo "</div>\n";
         }
         ?>
         <div class="form-group">
             <input type="submit" class="btn btn-secondary" value="Find Average of <?php echo $numberOfScores; ?> Highest Quiz Grades">
             <a href="." class="btn btn-secondary">Clear</a>
         </div>
     </form>
     <?php
     if (count($quizScores) === $numberOfScores){
         $total = 0;
         echo "<h2>Scores:</h2>\n";
         echo "<ul>\n";
         // for loop to print array elements and add each score to the total
         for ($i = 0; $i <= $gamesArrayElements; $i++){
             echo "<li>$quizScores[$i] " . getLetterGrade($quizScores[$i]) . "</li>\n";
             $total += $quizScores[$i];
         }
         echo "</ul>\n";
         $average = $total / $numberOfScores;
         echo "<h2>Average Score:</h2>\n";
         echo "<p>" . number_format($average, 1, .2) . " - " . getLetterGrade($average) . "</p>\n";
     } else {
         if(count($quizScores) > 0) {
             echo "<h2 class='text-danger'>All textboxes should contain grades.</h2>\n";
         }
     }
     ?>
 </div>
</div>
</body>
</html> ```

1 Answers1

0

I changed number_format($average, 1, .2) to number_format($average, 1) and then I do get the average rounded to one decimal. I also removed the getLetterGrade() function because it is not defined in your code. Finally I change the form action to "", instead of ".", because it didn't work in my test environment.

Perhaps you need to switch on error reporting in PHP? That way you can see what's wrong with your code.

KIKO Software
  • 10,480
  • 2
  • 13
  • 28