在php函数内递增

时间:2013-12-10 12:26:21

标签: php

我想知道你是否可以帮我解决我的php功能

我创建了一个使用单选按钮和提交按钮的测验。我希望该函数检查是否选择了特定的单选按钮,如果是,则在用户分数上添加1。但是没有添加任何功能,这是

    function Score()
{
    if(isset($_POST['correctAnswer'] ))

    {

        $answer=$_POST['correctAnswer'];
        $_SESSION['score']=$userScore+1;

    }

    else
    {
     $_SESSION['score']=$_SESSION['score'];

     }




}

以及提交表格

echo '<strong>'."$theQuestion".'</strong><br>';
   ?> <form name="correctAnswer" form method="post" action="quiz.php" onSubmit="Score()"> 
  <?php
   echo "$theAnswer1";?> <input type="radio" id="correct_answer" name="correctAnswer">
  <?php
   echo "<br>$theAnswer2"; ?> <input type="radio" id="wrong_answer1" name="wrongAnswer1"> 
   <?php
   echo "<br>$theAnswer3"; ?> <input type="radio" id="wrong_answer2" name="wrongAnswer2"> 
   <?php
   echo "<br>$theAnswer4"; ?> <input type="radio" id="wrong_answer3" name="wrongAnswer3"> 
   <input type="hidden" name="score" value="userScore">
   <br><input type="submit" value="Submit Answer">
   </form>

希望你能帮忙

2 个答案:

答案 0 :(得分:0)

您必须将计数器作为函数的参数传递,如此

 function Score($userScore){
    //Do the rest
 }

答案 1 :(得分:0)

看起来$ _SESSION ['得分']未定义

function Score() {

if (!isset($_SESSION['score'])) {
    $_SESSION['score'] = 0;
}
if (isset($_POST['correctAnswer'])) {

    $_SESSION['score']++;
}

}