I need to do this exercise:
Create a function workOutGradeAv
- this method will read in some grades, then work out and display the average grade.
Display a meaningful introductory message.
My code is this:
Assess1Grades.php
<!doctype html>
<html>
<head>
<title>Grade Assessor</title>
</head>
<body>
<h2>Grade Assessor<h2>
<table border="1">
<tr>
<td>
<table>
<form action="grades.php" method="post">
<tr>
<td>
Enter 5 grades. One per each line:
</td>
</tr>
<tr>
<td>
Grade 1: <input type="number" name="grade1" required>
</td>
</tr>
<tr>
<td>
Grade 2: <input type="number" name="grade2" required>
</td>
</tr>
<tr>
<td>
Grade 3: <input type="number" name="grade3" required>
</td>
</tr>
<tr>
<td>
Grade 4: <input type="number" name="grade4" required>
</td>
</tr>
<tr>
<td>
Grade 5: <input type="number" name="grade5" required>
</td>
</tr>
<tr>
<td align="center">
<input type="submit">
</td>
</tr>
</form>
</table>
</td>
</tr>
</table>
</body>
</html>
grades.php
<!doctype html>
<html>
<head>
<title>Grade Assessor</title>
</head>
<body>
<h2>Grade Assessor<h2>
<table border="1">
<tr>
<td>
<table>
<tr>
<td>
The Average grade is:
</td>
<td>
<?php
function workOutGradeAv() {
$averagegrade=$grades/5;
echo "$averagegrade";
}
$grades=0;
do {
if ($_Post["grade1"] < 0 or $_Post["grade1"] > 101) {
echo "Error! Check Input.";
} elseif ($_Post["grade2"] < 0 or $_Post["grade2"] > 101) {
echo "Error! Check Input.";
} elseif ($_Post["grade3"] < 0 or $_Post["grade3"] >101) {
echo "Error! Check Input.";
} elseif ($_Post["grade4"] < 0 or $_Post["grade4"] >101) {
echo "Error! Check Input.";
} elseif ($_Post["grade5"] < 0 or $_Post["grade5"]>101 ) {
echo "Error! Check Input.";
} else {
$grades = $grades + $_Post["grade1"] + $_Post["grade2"] + $_Post["grade3"] + $_Post["grade4"] + $_Post["grade5"];
}
}
while ($_Post["grade1"]!= 101);
workOutGradeAv();
?>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
When I submit the form the output is just The Average Grade is
and there is no result. What am I doing wrong?
You have multiple errors in grades.php
1)Remove unnecessary do while
loop
2)replace $_Post
to $_POST
.PHP
is case sensitive language.
3)to display grade in function you have to declare grades
as global. Or pass it as parameter.
<!doctype html>
<html>
<head>
<title>Grade Assessor</title>
</head>
<body>
<h2>Grade Assessor<h2>
<table border="1">
<tr>
<td>
<table>
<tr>
<td>
The Average grade is:
</td>
<td>
<?php
function workOutGradeAv($grades) {
$averagegrade=$grades/5;
echo "$averagegrade";
}
$grades=0;
if ($_POST["grade1"] < 0 || $_POST["grade1"] > 101) {
echo "Error! Check Input.";
} elseif ($_POST["grade2"] < 0 or $_POST["grade2"] > 101) {
echo "Error! Check Input.";
} elseif ($_POST["grade3"] < 0 or $_POST["grade3"] >101) {
echo "Error! Check Input.";
} elseif ($_POST["grade4"] < 0 or $_POST["grade4"] >101) {
echo "Error! Check Input.";
} elseif ($_POST["grade5"] < 0 or $_POST["grade5"]>101 ) {
echo "Error! Check Input.";
} else {
$grades = $grades + $_POST["grade1"] + $_POST["grade2"] + $_POST["grade3"] + $_POST["grade4"] + $_POST["grade5"];
}
workOutGradeAv($grades);
?>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>