PHP Form Example - Student Grading System

PHP Form Example - Student Grading System

  • HTML / PHP
  • 3 mins read

Here is a PHP program with four text fields and a submit button for calculating the total marks, displaying the grade for a student in a PHP file.

PHP Form Example

In this program, we are creating a <table> tag (defining a table on a web page), <tr> tag(defining a row for a table), <td>tag(defining a cell/data of a table), <center> tag(here it is defining the HTML element in the middle of the web page) and we are passing 4 values ("John Mathew" for first text box, 90 is for second text field, 80 is for third text field, 70 is for 4th text field) and hereafter clicking on submit button, we are getting the result. Here <input type=submit> is defining a submit button.

<html>
    <body>
        <form action="" method="post">
            <center>
                <table border=0>
                    <tr>
                        <td>
                            Student Name
                        </td>
                        <td>
                            <input type=text name="t1">
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Marks for Physics
                        </td>
                        <td>
                            <input type=text name="t2">
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Marks for Chemistry
                        </td>
                        <td>
                            <input type=text name="t3">
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Marks for Math
                        </td>
                        <td>
                            <input type=text name="t4">
                        </td>
                    </tr>
                </table>
                <br>
                <br>
                <input type=submit name="s" value="Result">
            </center>
            <?php
if(isset($_POST['s']))////checking whether the input element is set or not
{
    $a=$_POST['t1']; //accessing value from 1st text box
    $a1=$_POST['t2']; //accessing value from 2nd text field
    $a2=$_POST['t3']; //accessing value from 3rd text field
    $a3=$_POST['t4']; //accessing value from 4th text field
    $sum=$a1+$a2+$a3; //total marks
    $avg=$sum/3;
    if($avg>=0&&$avg<=50)
        $grade="Fail";
    if($avg>50&&$avg<=70)
        $grade="C";
    if($avg>70&&$avg<=80)
        $grade="B";
    if($avg>80&&$avg<=90)
        $grade="A";
    if($avg>90)
        $grade="E";
    echo "<br>";
    echo "<font size=4><center>Student is:-".$a."</center><br>"; 
    echo "<font size=4><center>Total marks:-".$sum."</center><br>"; 
    echo "<font size=4><center>Grade is:-".$grade."</center>"; 
}
            ?>
        </form>
    </body>
</html>

Output (as shown in the featured image of this article)

Student is:- John Mathew

Total marks:- 240

The grade is:- B

This program is containing some conditions, based on one condition using an if-else statement we are displaying the grade of the student.