2013-12-10 119 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']++; 
} 

}