未定义变量:totalScore。我该如何定义这个变量,因为它是计算的结果?
Undefined variable: totalScore. How do I define this variable since it is a result of a calculation?
这是有问题的代码行。我需要定义 $totalScore 但不确定要使用哪种方法,因为它是一个结果变量。我尝试将其设置为“0”“”和 "null",但这些值并没有解决问题。这里有人知道这个问题的解决方案,所以我可以显示收到的成绩作为警报吗?
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!");} }
下面是剩余的代码供参考!
<?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>
看到这个。您代码的改进版本
首先,您不能直接在内部使用 javascript 警报。
<?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>
我做了一些更改请看下面的代码,实际上你必须在你的 post 条件之前声明 $totalscore 并且整个代码应该在条件中。
<?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!");} }
?>
变量$totalScore
不会被初始化,当这个条件if(isset($_POST['submit']))
为假时,因为你的代码中有太多{
和}
。所以我会检查为什么 post 变量 submit
没有设置,更重要的是,在另一种情况下也初始化变量。
您仅在提交表单后才初始化 $totalScore
,首先您应该从一开始就用 null
初始化它
<?php
$totalScore = null;
if (isset($_POST['submit']) {
//...
} elseif ($totalScore && (/*your initial conditions*/)) {
//calculate marks
}
如果您想要更简洁的设计,我建议您使用 Ajax 作为回应,您会提示结果;
这是有问题的代码行。我需要定义 $totalScore 但不确定要使用哪种方法,因为它是一个结果变量。我尝试将其设置为“0”“”和 "null",但这些值并没有解决问题。这里有人知道这个问题的解决方案,所以我可以显示收到的成绩作为警报吗?
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!");} }
下面是剩余的代码供参考!
<?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>
看到这个。您代码的改进版本
首先,您不能直接在内部使用 javascript 警报。
<?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>
我做了一些更改请看下面的代码,实际上你必须在你的 post 条件之前声明 $totalscore 并且整个代码应该在条件中。
<?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!");} }
?>
变量$totalScore
不会被初始化,当这个条件if(isset($_POST['submit']))
为假时,因为你的代码中有太多{
和}
。所以我会检查为什么 post 变量 submit
没有设置,更重要的是,在另一种情况下也初始化变量。
您仅在提交表单后才初始化 $totalScore
,首先您应该从一开始就用 null
初始化它
<?php
$totalScore = null;
if (isset($_POST['submit']) {
//...
} elseif ($totalScore && (/*your initial conditions*/)) {
//calculate marks
}
如果您想要更简洁的设计,我建议您使用 Ajax 作为回应,您会提示结果;