I have this easy programming exercise and I'm trying to do it in PHP: Determine if a student pass or not the course. The student will pass the course if his average of three grades is greater than or equal to 15. Show a message if the student passes or not and validate the three grades before you calculate the average. Show an error message if any grade if wrong.
I have it in both pseudo-code and pseint, so it is easy to validate because I use a do-while loop and everytime it asks me for typing a number and I do it wrong, it validates the grades:
Algorithm Exercise14
Define grade1, grade2, grade3, prom as Real;
Repeat
    print "Type the first grade: ";
    read grade1;
    if (grade1<0 | grade1>20) 
        print "Error! Invalid grade, type a valid grade.";
    endif
Until (grade1>=0 & grade2<=20);
Repeat
    print "Type the second grade: ";
    read grade2;
    if (grade2<0 | grade2>20) 
        print "Error! Invalid grade, type a valid grade.";
    endif
Until (grade2>=0 & grade2<=20);
Repeat
    print "Type the third grade: ";
    read grade3;
    if (grade3<0 | grade3>20) 
        print "Error! Invalid grade, type a valid grade.";
    endif
Until (grade3>=0 & grade3<=20);
prom = (grade1+grade2+grade3)/3;
if (prom>=15) 
    print "The student passed the course!";
    print "The average is: " prom;
else
    print "The student did not pass the course.";
    print "The average is: " prom;
endif
endAlgorithm
Now, I want to do this but in PHP. My problem is that I cannot validate in this way because if I do it, the browser just takes a lot of time to react and it does nothing, so I have looked on the internet and I need some function to validate but I'm not sure. I'm learning PHP so, excuse me if this is a repeated question or it is to easy but I want to know because I'm learning.
The final question is, how can I validate those fields if I do it with PHP and obviously, HTML5?
PHP code:
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Exercise 14</title>
    <meta charset="utf-8">
</head>
<body>
    <h2>PHP Exercises</h2>
    <h3>Exercise 14</h3>
    <form action="#" method="post" id="constant" name="constant">
        <div>
            <label for="text">Grade 1: </label>
    <input type="text" name="grade1" placeholder="Type grade1">
        </div>
        <div>
            <label for="text">Grade 2: </label>
        <input type="text" name="grade2" placeholder="Type grade 2">
        </div>
        <div>
            <label for="text">Grade 3: </label>
        <input type="text" name="grade3" placeholder="Type grade 3">
        </div>
        <div>
      <input type="submit" name="button" id="sending" value="Send">
        </div>
    </form>
    <?php
        if(isset($_POST['button'])){
            $grade1 = $_POST['grade1'];
            $grade2 = $_POST['grade2'];
            $grade3 = $_POST['grade3'];
            while($grade1<0){
                    echo "Error!";
            }
            do{
                if ($grade2<0 | $grade2>20) {
                    echo "Error!";
                }
            }while($grade2>=0 && $grade2<=20);
            do{
                if ($grade3<0 | $grade3>20) {
                    echo "Error!";
                }
            }while($grade3>=0 && $grade3<=20);
            $prom = ($grade1+$grade2+$grade3/3);
            if ($prom>=15) {
                echo "The averages is: " . $prom;
                echo "The student passed the course!";
            }else{
                echo "The averages is: " . $prom;
                echo "The student did not pass the course.";
            }
        }
    ?>
 </body>
</html>
 
    