duandang9434 2015-06-16 06:16
浏览 164
已采纳

未定义的变量: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>
  • 写回答

4条回答 默认 最新

  • dsgtew3241 2015-06-16 06:34
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?