未定义的变量:totalScore。 如何定义此变量,因为它是计算的结果?

未定义的变量:totalScore。 如何定义此变量,因为它是计算的结果?

问题描述:

Here is the line of code that is in question. I need to define $totalScore but am not sure which method to use, as it is a result variable. I've tried setting it to "0" " " and "null", but those values did not solve the problem. Anyone here know the solution to this so I can display the grade received as an alert?

if ($totalScore >= 900 && $totalScore <= 1000) {alert("Total Points of "+$totalScore+" gives you an A!");} 
        else if ($totalScore >= 800 && $totalScore <= 899) {alert("Total Points of "+$totalScore+" gives you an B!");}  
        else if ($totalScore >= 700 && $totalScore <= 799) {alert("Total Points of "+$totalScore+" gives you an C!");} 
        else if ($totalScore >= 600 && $totalScore <= 699) {alert("Total Points of "+$totalScore+" gives you an D!");} 
        else if ($totalScore >= 0 && $totalScore <= 599) {alert("Total Points of "+$totalScore+" gives you an F!");} } 

Here's the rest of the code for reference!

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>  
<?php
if(isset($_POST['submit'])) {
$name = $_POST['StudentName'];
$quiz = (int)$_POST['QuizScore'];
$assignment = (int)$_POST['AssignmentScore'];
$midterm = (int)$_POST['MidtermScore'];
$final = (int)$_POST['FinalScore'];
$totalScore = NULL ;
$totalScore = $quiz + $assignment + $midterm + $final;
echo "Total Score: " , $totalScore; }
{
if ($totalScore >= 900 && $totalScore <= 1000) {alert("Total Points of "+$totalScore+" gives you an A!");} 
        else if ($totalScore >= 800 && $totalScore <= 899) {alert("Total Points of "+$totalScore+" gives you an B!");}  
        else if ($totalScore >= 700 && $totalScore <= 799) {alert("Total Points of "+$totalScore+" gives you an C!");} 
        else if ($totalScore >= 600 && $totalScore <= 699) {alert("Total Points of "+$totalScore+" gives you an D!");} 
        else if ($totalScore >= 0 && $totalScore <= 599) {alert("Total Points of "+$totalScore+" gives you an F!");} }  

?>
<!DOCTYPE html>
<html>
<head>
<body>
<div align='center'><form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<input type="text" id="studentName" size="25px" placeholder="Please enter your name" name="StudentName"><br><br>
Total of Quiz Scores (150 possible):<input type="text" id="QuizScore" name="QuizScore" min="1" max="150" size="5"/><br> 
Total of Assignment Scores (400 possible):<input type="text" id="AssignmentScore" name="AssignmentScore" min="1" max="400" size="5"><br> 
Midterm Score (200 possible):<input type="text" id="MidtermScore" name="MidtermScore" min="1" max="200" size="5"><br> 
Final Score (250 possible):<input type="text" id="FinalScore" name="FinalScore" min="1" max="250" size="5"><br> 
<input type="submit" value="Calculate your score" name="submit"><br><br>
</form></div>
</body>
</html>

See this. improved version of your Code

first thing first you cannot use javascript alert directly inside.

<?php
   if(isset($_POST['submit'])) 
   {
    $name = $_POST['StudentName'];
     $quiz = (int)$_POST['QuizScore'];
     $assignment = (int)$_POST['AssignmentScore'];
     $midterm = (int)$_POST['MidtermScore'];
     $final = (int)$_POST['FinalScore'];
    $totalScore = 0;;
    $totalScore = $quiz + $assignment + $midterm + $final;
     // echo "Total Score: " , $totalScore; 
     if ($totalScore >= 900 && $totalScore <= 1000) { 
        $message = "Total Points of ".$totalScore." gives you an A!"; 
    } 
    else if ($totalScore >= 800 && $totalScore <= 899) { 
        $message = "Total Points of ".$totalScore." gives you an B!" ;
   }  
    else if ($totalScore >= 700 && $totalScore <= 799) { 
        $message = "Total Points of ".$totalScore." gives you an C!"; 
    } 
   else if ($totalScore >= 600 && $totalScore <= 699) { 
        $message = "Total Points of ".'.$totalScore.'." gives you an D!"; 
   } 
    else if ($totalScore >= 0 && $totalScore <= 599) { 
        $message = "Total Points of ".$totalScore." gives you an F!";
    } 
   echo '<script type="text/javascript">  alert("'.$message.'");        </script>'; 
  }
   ?>

<html>
<head>
<body>
<div align='center'><form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >

 <input type="text" id="studentName" size="25px" placeholder="Please enter your name" name="StudentName"><br><br>

 Total of Quiz Scores (150 possible):<input type="text" id="QuizScore" name="QuizScore" min="1" max="150" size="5"/><br> 

 Total of Assignment Scores (400 possible):<input type="text" id="AssignmentScore" name="AssignmentScore" min="1" max="400" size="5"><br>

 Midterm Score (200 possible):<input type="text" id="MidtermScore" name="MidtermScore" min="1" max="200" size="5"><br> 

  Final Score (250 possible):<input type="text" id="FinalScore" name="FinalScore" min="1" max="250" size="5"><br> 

 <input type="submit" value="Calculate your score" name="submit">
 <br><br>
 </form>
 </div>
 </body>
</html>

I have made some changes please have look on below code, actually you have to declare $totalscore before your post condition and whole code should be in condition.

<?php
 $totalScore= 0;
if(isset($_POST['submit'])) {
$name = $_POST['StudentName'];
$quiz = (int)$_POST['QuizScore'];
$assignment = (int)$_POST['AssignmentScore'];
$midterm = (int)$_POST['MidtermScore'];
$final = (int)$_POST['FinalScore'];
global $totalScore;
$totalScore = $quiz + $assignment + $midterm + $final;
echo "Total Score: " , $totalScore; 

if ($totalScore >= 900 && $totalScore <= 1000) {alert("Total Points of "+$totalScore+" gives you an A!");} 
        else if ($totalScore >= 800 && $totalScore <= 899) {alert("Total Points of "+$totalScore+" gives you an B!");}  
        else if ($totalScore >= 700 && $totalScore <= 799) {alert("Total Points of "+$totalScore+" gives you an C!");} 
        else if ($totalScore >= 600 && $totalScore <= 699) {alert("Total Points of "+$totalScore+" gives you an D!");} 
        else if ($totalScore >= 0 && $totalScore <= 599) {alert("Total Points of "+$totalScore+" gives you an F!");} }  

?>

</div>

Variable $totalScore will not be initialized, when this condition if(isset($_POST['submit'])) is false, because there are too many { and } in your code. So i would check why the post variable submit is not set, and even more important, initialize the variable in the other case too.

You are initializing the $totalScore only if the form was submitted, first you should initialize it with null right from start

<?php
  $totalScore = null;

  if (isset($_POST['submit']) {
     //...
  } elseif ($totalScore && (/*your initial conditions*/)) {
     //calculate marks
  }

If you'd like a more cleaner design i suggest you to use Ajax and in response you will alert the result;