PHP测验代码(单选按钮不会更新全局值)

时间:2014-06-07 20:50:26

标签: php

我正在制作一个网页,向用户提出15个问题。根据这15个问题的答案,网页将确定用户将弯曲的元素类型(它是对“阿凡达最后的气宗”的引用)。

我希望网站首先显示一个描述页面($ currentPage = 0)。然后,当用户单击“下一个问题”按钮时,$ currentPage变量将增加1,第一个问题将出现,其下有四个单选按钮作为可能的答案。每个答案对应一个元素(空气,土,水或火)。如果用户选择(仅一个单选按钮),该元素的分数将增加1.然后当用户单击“下一个问题”按钮时,$ currentPage变量将再次增加1并显示下一个问题。在15个问题的结尾(总共16页,包括描述页面),我将比较所有四个分数,看看哪个值最大,以确定用户将弯曲的元素。

我只编写了1个问题和描述部分,但在选择与答案对应的单选按钮并点击下一个问题后,我似乎无法将$ earthScore,$ waterScore,$ fireScore或$ airScore增加1

你们能帮帮我吗???

我没有10个声望点,所以这里是网页截图的链接:

Description

Question 1

以下是来自benderTest.php文件的PHP代码:

    <!-- Begin Test frame -->
    <form action="benderTest.php" method="get" enctype="multipart/form-data">         
    <div class="benderTest">
    <input class="benderNext" type="submit" value="Next Question"/>        
    <?php
    $currentPage = (int)$_GET['currentPage'];
    $waterScore = (int)$_GET['waterScore'];
    $earthScore = (int)$_GET['earthScore'];
    $airScore = (int)$_GET['airScore'];
    $fireScore = (int)$_GET['fireScore'];        

    if($currentPage == 0)  
    {      
        echo "<p class='benderQuestion'>Discover what kind of bender you are! Click next question.</p>";
    }
    else if($currentPage == 1)
    {
        echo "<p class='benderQuestion'>Which battle strategy do you most prefer?</p><br/><br/><br/>"; 
        echo "<input style='margin: 15px;' type='radio' name='earthScore' value='".($earthScore+1)."'><font class='benderAns'>Waiting for the right moment to strike.</font><br><br>";
        echo "<input style='margin: 15px;' type='radio' name='airScore' value='".($airScore+1)."'><font class='benderAns'>Avoid and evade.</font><br><br>";
        echo "<input style='margin: 15px;' type='radio' name='waterScore' value='".($waterScore+1)."'><font class='benderAns'>Turning my opponent's force against him/her.</font><br><br>";
        echo "<input style='margin: 15px;' type='radio' name='fireScore' value='".($fireScore+1)."'><font class='benderAns'>Go completely on the offensive.</font><br><br>";                       
    }  

    $currentPage = $currentPage + 1;  
    ?>       
    <input class="benderHide" name ="currentPage" type="text" value="<?php echo $currentPage; ?>"/>
    </div>  
    </form>            
    <!-- End test frame -->

2 个答案:

答案 0 :(得分:2)

不要将您的值放在输入中,而是将结果保存在$_SESSION

<?php
// start session at the top of page, before other code
session_start();

// set values on initial page
if(!isset($_GET['currentPage'])) {
    $_SESSION['air'] = 0;
    $_SESSION['earth'] = 0;
    $_SESSION['fire'] = 0;
    $_SESSION['water'] = 0;
}
?>

然后如果检查了该值,请增加该值(注意,我更改了您的无线电名称,请参阅最后一个代码块)

$safe_values = array('air','earth','fire','water');

// increase checked value (sanitizing data by checking if field is set, white listed in safe array and if it is a valid Session key)
if(isset($_GET['score']) && in_array($_GET['score'],$safe_values) && isset($_SESSION[$_GET['score']]){
    $_SESSION[$_GET['score']]++;
}

现在,在您的表单中,使用相同的广播名称 - &gt; name="score",并将值设置为元素

else if($currentPage == 1)
    {
        echo "<p class='benderQuestion'>Which battle strategy do you most prefer?</p><br/><br/><br/>"; 
        echo "<input style='margin: 15px;' type='radio' name='score' value='earth'><font class='benderAns'>Waiting for the right moment to strike.</font><br><br>";
        echo "<input style='margin: 15px;' type='radio' name='score' value='air'><font class='benderAns'>Avoid and evade.</font><br><br>";
        echo "<input style='margin: 15px;' type='radio' name='score' value='water'><font class='benderAns'>Turning my opponent's force against him/her.</font><br><br>";
        echo "<input style='margin: 15px;' type='radio' name='score' value='fire'><font class='benderAns'>Go completely on the offensive.</font><br><br>";                       
    }  

答案 1 :(得分:1)

在这里你去:P将其粘贴到随机的php文件中,看看它是如何工作的。

我要困难atm写完整的解释,但如果你需要一个,我会在早上。

<!-- Begin Test frame -->
    <form action="" method="post">         
    <div class="benderTest">
    <input class="benderNext" type="submit" value="Next Question"/>        
    <?php

    $currentPage = (int)$_POST['currentPage'];

    $waterScore = (int)$_POST['waterScoreData'];
    $earthScore = (int)$_POST['earthScoreData'];
    $airScore = (int)$_POST['airScoreData'];
    $fireScore = (int)$_POST['fireScoreData']; 



   isset ($_POST['waterScore']) ? $waterScore++ : $waterScore;
   isset ($_POST['earthScore']) ? $earthScore++ : $earthScore;
   isset ($_POST['airScore']) ? $airScore++ : $airScore;
   isset ($_POST['fireScore']) ? $fireScore++ : $fireScore;        


//Question array
    $questions = array (
        "Discover what kind of bender you are! Click next question.",
        "Which battle strategy do you most prefer?",
        "Question number 2! Click next question.",
        "Question number 3! Click next question.",
    );

    $earthAnwser = array (
            1 => "Waiting for the right moment to strike.", 
            2 => "Anwser 2", 
            3 => "anwser 3"
            );
    $airAnwser= array (
        1 => "Avoid and evade.", 
        2 => "Anwser 2", 
        3 => "anwser 3"
        );
    $waterAnwser = array (
        1 => "Turning my opponent's force against him/her.", 
        2 => "Anwser 2", 
        3 => "anwser 3");
    $fireAnwser = array (
        1 => "Go completely on the offensive.", 
        2 =>"Anwser 2", 
        3 => "anwser 3");


        isset($currentPage) ? $currentPage : $currentPage = 0;



        if ($currentPage) { // if current page is bigger than 0 
        echo "<p class='benderQuestion'>" . $questions[$currentPage]. "</p><br/><br/><br/>"; 
        echo "<input style='margin: 15px;' type='radio' name='earthScore' value='1'><font class='benderAns'>". $earthAnwser[$currentPage]."</font><br><br>";
        echo "<input style='margin: 15px;' type='radio' name='airScore' value='1'><font class='benderAns'>". $airAnwser[$currentPage] ."</font><br><br>";
        echo "<input style='margin: 15px;' type='radio' name='waterScore' value='1'><font class='benderAns'>".  $waterAnwser[$currentPage] ."</font><br><br>";
        echo "<input style='margin: 15px;' type='radio' name='fireScore' value='1'><font class='benderAns'>".  $fireAnwser[$currentPage]."</font><br><br>";                       
        ?>
        <input type="hidden" name="waterScoreData" value="<?php echo $waterScore ;?>">
        <input type="hidden" name="earthScoreData" value="<?php echo $earthScore ;?>">
        <input type="hidden" name="airScoreData" value="<?php echo $airScore ;?>">
        <input type="hidden" name="fireScoreData" value="<?php echo $fireScore ;?>">
        <?php
        } else {

           echo "<p class='benderQuestion'>" .$questions[$currentPage]. "</p><br/><br/><br/>";  
        }

    $currentPage = $currentPage + 1;  
    ?>       

    <input type="hidden" name ="currentPage" type="text" value="<?php echo $currentPage; ?>"/>
    <?php 

        if ($earthScore) echo "<br/>Earth Score: " . $earthScore . "<br/>";
        if ($airScore)  echo "Air Score: " . $airScore . "<br/>";
        if ($waterScore) echo "Water Score: ". $waterScore . "<br/>";
        if ($fireScore) echo "Fire: Score " . $fireScore . "<br/>";
        ?>
    </div>  
    </form>            
    <!-- End test frame -->