PHP Programming Help!?
Ok, I’m doing an assignment for school that is really confusing me. I made an htm form that is supposed to leave a box for you to enter a grade letter, and after doing so, you will get a different result depending on the letter you typed. I made the PHP file, but no matter what grade I type, it gives me the same result every time, "Your grade is excellent!". I need help with making it give me different results depending on what I type, and I think the main problem is my call statement.
This is my html form (which is fine):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Letter Grades</title>
</head>
<body>
<form action="LetterGrades.php" method="get">
<p>Grade: <input type="text" name="Grade" />
<input name="Submit" type="submit" id="Submit" />
</p>
</form>
</body>
</html>
And this is my PHP file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Letter Grades</title>
</head>
<body>
<?php
function checkGrade($Grade) {
switch ($Grade) {
case "A":
echo "Your grade is excellent.";
break;
case "B":
echo "Your grade is good.";
break;
case "C":
echo "Your grade is fair.";
case "D":
echo "You are barely passing.";
case "F":
echo "You failed.";
default:
echo "You did not enter a valid letter grade.";
}
}
echo checkGrade("A");
?>
</body>
</html>
Could someone please help me?
Thanks, Frank! I think it’s a step in the right direction, but the only problem now is that it’s giving me the default answer everytime now, which is, "You did not enter a valid letter grade."
Do I need to define the A, B, C, D, F variables anywhere?
Ok, it works now, thank you, Frank!!! ![]()
I think you wrote a good script. The problem with this script i think will be at the last statement (echo checkGrade("A");).
Instead of you writing echo checkGrade("A"); I suggest you write
echo checkGrade("$Grade");
Ooh plus you haven’t linked the form with the php script
write something like:
<?php
function checkGrade($Grade) {
$Grade = $_GET['Grade'];
switch ($Grade) {
case "A":
echo "Your grade is excellent.";
break;
case "B":
echo "Your grade is good.";
break;
case "C":
echo "Your grade is fair.";
case "D":
echo "You are barely passing.";
case "F":
echo "You failed.";
default:
echo "You did not enter a valid letter grade.";
}
}
echo checkGrade("$Grade");
?>
I think you wrote a good script. The problem with this script i think will be at the last statement (echo checkGrade("A");).
Instead of you writing echo checkGrade("A"); I suggest you write
echo checkGrade("$Grade");
Ooh plus you haven’t linked the form with the php script
write something like:
<?php
function checkGrade($Grade) {
$Grade = $_GET['Grade'];
switch ($Grade) {
case "A":
echo "Your grade is excellent.";
break;
case "B":
echo "Your grade is good.";
break;
case "C":
echo "Your grade is fair.";
case "D":
echo "You are barely passing.";
case "F":
echo "You failed.";
default:
echo "You did not enter a valid letter grade.";
}
}
echo checkGrade("$Grade");
?>
References :