通过javascript函数增加php得分变量

时间:2013-12-09 21:13:33

标签: javascript php jquery

嗨,大家好,我想知道你是否可以帮助我在我的代码中增加一个得分变量。我目前正在使用php编写在线测验并且希望每次选择提交按钮时分数增加1并且选中特定单选按钮但是当变量传递到结果页面时它会变为0

主要测验页面

<?php session_start(); ?>
<html>
<head>
<title> World Cup Quiz  </title>
</head>

<script>

function Score()
{
    if(document.getElementById('correct_answer').checked) 
    {
      <?
        $_SESSION['score']=$userScore]+1;
        ?>
    }
    else if(document.getElementById('wrong_answer1').checked) 
    {
      //Wrong 1 radio button is checked
    }
    else if(document.getElementById('wrong_answer2').checked) 
    {
      //Wrong 2 radio button is checked
    }
    else if(document.getElementById('wrong_answer3').checked) 
    {
      //Wrong 3 radio button is checked
    }

}

</script>


<body>
<div align = center><strong> World Cup Quiz</strong></div>
<br />

<div align =center>
<?php



include ("dbConnect.php");

if (!isset($_SESSION['number']))
{
    $_SESSION['number']=1;
}

if (!isset($_SESSION['score']))

{
    $_SESSION['score']=0;

}


$questionNumber = $_SESSION['number'];
$userScore=$_SESSION['score'];
$number= rand(1,4);

//search database for generated number and match ID
$dbQuery= "SELECT * FROM `questions 1.0` WHERE  `ID` =$number";
$dbResult=mysql_query($dbQuery);

echo "Question:".$questionNumber."/5<br>";

//Assign variables to each attribute

while ($dbRow=mysql_fetch_array($dbResult))

{
 if ($_SESSION['number']>5)

    {
        header("Location: results.php");
        $_SESSION['number']=1;
        break;


    }

   $theID=$dbRow["ID"];
   $theQuestion=$dbRow["Question"];
   $theAnswer1=$dbRow["Correct Answer"];
   $theAnswer2=$dbRow["Wrong Answer 1"];
   $theAnswer3=$dbRow["Wrong Answer 2"];
   $theAnswer4=$dbRow["Wrong Answer 3"];
   $_SESSION['number']=$questionNumber+1;


}

  //Print Questions and Answers


    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>


</div>
</body>

</html>

结果页面

<?php session_start(); ?>
<html>

</head>

<title> Result</title>

<body>

    <?php

    $score=$_SESSION['score'];

    echo "Congratulations you scored $score /5"

    ?>

    <form action="menu.php">

    <input type="submit" value="Return to Main Menu">
    </form>

</body>

</html>

希望你能帮忙

由于

2 个答案:

答案 0 :(得分:0)

您无法使用客户端javascript设置php会话变量。 你的javascript必须提交到服务器端的php文件(带有ajax),可以返回新的值

答案 1 :(得分:0)

您可以将分数存储在js和php都可用的cookie中。

javascript方面:

<script type="text/javascript">
   // When starting, set the score to 0.
   setCookie("score", 0, minutesuntilexpires)

   function score(){
    ...
        minutesuntilexpires = 60;
        score = getcookie("score");
        setCookie("score", score + 1, minutesuntilexpires);
    ...
    }

function setCookie(name, value, minutes) {
    minutes = typeof minutes !== 'undefined' ? minutes : 60 ;
    var date= new Date();
    if (minutes < 0){       
        expires = "Thu, 01 Jan 1970 00:00:01 GMT";
    }
    else{
      date.setTime(date.getTime()+(minutes*60*1000));
        expires = date.toUTCString();
    }        

    var value = escape(value) + "; expires="+expires;

    document.cookie=name + "=" + value + '; path=/';
}

function getCookie(name) {
    var value = document.cookie;
    var start = value.indexOf(" " + name + "=");
    if (start == -1) {
        start = value.indexOf(name + "=");
    }
    if (start == -1) {
        value = null;
    }
    else {
        start = value.indexOf("=", start) + 1;
        var end = value.indexOf(";", start);
        if (end == -1) {
            end = value.length;
        }
        value = unescape(value.substring(start,end));
    }
    return value;
}

</script>

php方面:

if (isset($_COOKIE['score'])){
    $score = $_COOKIE['score'];
}