2013-12-10 71 views
-3

里面我想知道如果你能帮助我与我的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> 

希望你能帮助

+1

什么是'$ userScore'? –

+0

'$ _SESSION ['score'] = $ _ SESSION ['score']'完全没有。 – deceze

回答

0

你必须到柜台作为参数传递这样的功能

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

我试过这样做,但它仍然在文档的末尾打印为0。 – shd0w

0

看起来像$ _SESSION ['score']是未定义的

function Score() { 

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

    $_SESSION['score']++; 
} 

}