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
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.";
}
}
?>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.