Jump to content

[SOLVED] Help Calling this function, plz!


y0y0y0

Recommended Posts

Hello. I am attempting to try my luck at PHP. I can not figure out how to call the function in the PHP code. I keep getting a blank page.

 

I am using a simple html form with an input field and a submit button. When you enter in a particular letter grade, it should return a response based on their answer. Here are the bits of code. Thanks for any assistance.

 

HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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 type="submit" />
</p>
</form>
</body>
</html>

 

PHP:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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.";
         break;
         
      case "D":
         echo "You are barely passing.";
         break;
         
      case "F":
         echo "You failed.";
         break;
      default:
         return "You did not enter a valid letter grade.";
}
}
?>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/159940-solved-help-calling-this-function-plz/
Share on other sites

Put this in a single file called LetterGrades.php

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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 type="submit" />
</p>
</form>
<?php
if(!empty($_GET['grade'])) checkGrade($_GET['grade']);
?>
</body>
</html>
<?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.";
         break;
         
      case "D":
         echo "You are barely passing.";
         break;
         
      case "F":
         echo "You failed.";
         break;
      default:
         return "You did not enter a valid letter grade.";
}
}
?>

Your very welcome. (and welcome to PHPFreaks)

 

Oh and just so you know, without a function call it would look like this

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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 type="submit" />
</p>
</form>
<?php
if(!empty($_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.";
		 break;

	  case "D":
		 echo "You are barely passing.";
		 break;

	  case "F":
		 echo "You failed.";
		 break;
	  default:
		 return "You did not enter a valid letter grade.";
	}
}
?>
</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.