提交测验时,脚本会为评分添加新问题,而不是提交问题

时间:2019-05-06 06:23:35

标签: php

我正在创建一个web form的全部功能,该功能可以从文件中检索10 random questionsanswer key,并将它们作为测验显示给用户,然后对结果进行评分。我遇到的problem是,当进行submitting测验时,程序会尝试将separate set of questionsanswers与输入进行比较,而不是对原始问题进行评分。我知道代码远非完美,但我确实需要帮助了解为什么它会继续重建测验。

我没有从当前位置调用get_questions()函数,而是尝试将其放在display_form()函数的开头,这导致了某种不确定的错误。我也尝试过使用各种if语句来调用get_questions()函数,希望使该函数仅运行一次。什么都没有。

代码

function get_questions(){

    $TFQuestArray = file("TFQuestions.txt");
    $TFAnswersArray = file("TFAnswers.txt");
    $Tmp = array_combine($TFQuestArray, $TFAnswersArray);

    $Keys = array_keys($Tmp);
    shuffle($Keys);

    foreach ($Keys as $Key)
     {
       $TFShuffledArray[$Key] = $Tmp[$Key];
     }
     $TFQuestArray = array_keys($TFShuffledArray);
     $TFAnswersArray = array_values($TFShuffledArray);

     // this section of code repeats the above process with the Fill in the blank Q and A's
        $FIBQuestArray = file("FIBQuestions.txt");
        $FIBAnswersArray = file("FIBAnswers.txt");
        $Tmp = array_combine($FIBQuestArray, $FIBAnswersArray);

        $Keys = array_keys($Tmp);
        shuffle($Keys);

        foreach ($Keys as $Key)
        {
            $FIBShuffledArray[$Key] = $Tmp[$Key];
        }

        $FIBQuestArray = array_keys($FIBShuffledArray);
        $FIBAnswersArray = array_values($FIBShuffledArray);

// in any event the code goes on but it builds the questions just fine, it pulls 4 True False, 3  Fill in the Blank, and 3 Multiple choice and creates an array containing the proper values.

        $GetQuestionsTF = array_slice($TFQuestArray, 0, 4);
        $GetQuestionsMC = array_slice($MyChoiceQuestions, 0, 3);
        $GetQuestionsFIB = array_slice($FIBQuestArray, 0, 3);
        global $QuizQuestions;
        $QuizQuestions = array_merge($GetQuestionsTF, $GetQuestionsMC,           $GetQuestionsFIB);
        $GetCorrectAnswersTF = array_slice($TFAnswersArray, 0, 4);
        $GetCorrectAnswersMC = array_slice($MCAnswersArray, 0, 3);
        $GetCorrectAnswersFIB = array_slice($FIBAnswersArray, 0, 3);
        global $QuizAnswers;
        $QuizAnswers = array_merge($GetCorrectAnswersTF,$GetCorrectAnswersMC, $GetCorrectAnswersFIB);

// this is the display form function
function displayForm($User, $UserEmail, $RevEmail, $Message, $QuizQuestions, $AnswerOne, $AnswerTwo, $AnswerThree, $AnswerFour, 
    $AnswerFive, $AnswerSix, $AnswerSeven, $AnswerEight, $AnswerNine, $AnswerTen, $QuizChoices, $QuizAnswers) {

        ?> <!-- Closes the initial php close block to allow advanced escaping
                notice how often php is open and closed within the form-->

        <h2 style = "text-align:center">Chinese Zodiac Assesment</h2>
        <form name="Final Quiz" action="FinalQuiz.php" method="post">
        <p>Your Name: <input type="text" name="User" value="<?php
        echo $User; ?>" /></p>
        <p>Your E-mail: <input type="text" name="UserEmail" value="<?php
        echo $UserEmail; ?>" /> </p>
        <p>Reviewers E-mail: <input type="text" name="RevEmail" value="<?php
        echo $RevEmail; ?>"  /></p>
        <p>Message:<br />
        <textarea name="Message"><?php echo $Message;
            ?></textarea></p>
        <br>
        <?php echo "<table border=\"1\"width=\"50%\">\n";
        echo "<tr>\n";
        echo "<td><p>Question 1 - True or False:</p>\n"; 
        echo  htmlentities($QuizQuestions[0]) . "\n";
        echo "<p>Answer: "?> <input type="radio" name="answerone" <?php
        if (isset($AnswerOne) && $AnswerOne =="T") echo "checked"; ?> 
        value="T">True
        <input type="radio" name="answerone" <?php
        if (isset($AnswerOne) && $AnswerOne =="F") echo "checked"; ?>
        value="F">False</p> . <?php "</td>";
        echo "</tr>\n";
        echo "</table>\n";?></p>
        <?php echo "<table border=\"1\"width=\"50%\">\n";
        echo "<tr>\n";
        echo "<td><p>Question 2 - True or False:</p>\n"; 
        echo  htmlentities($QuizQuestions[1]) . "\n";
        echo "<p>Answer: "?> <input type="radio" name="answertwo" <?php
        if (isset($AnswerTwo) && $AnswerTwo =="T") echo "checked"; ?> 
        value="T">True
        <input type="radio" name="answertwo" <?php
        if (isset($AnswerTwo) && $AnswerTwo =="F") echo "checked"; ?>
        value="F">False</p> . <?php "</td>";
        echo "</tr>\n";
        echo "</table>\n";?></p>
        <?php echo "<table border=\"1\"width=\"50%\">\n";
        echo "<tr>\n";
        echo "<td><p>Question 3 - True or False:</p>\n"; 
        echo  htmlentities($QuizQuestions[2]) . "\n";
        echo "<p>Answer: "?> <input type="radio" name="answerthree" <?php
        if (isset($AnswerThree) && $AnswerThree =="T") echo "checked"; ?> 
        value="T">True
        <input type="radio" name="answerthree" <?php
        if (isset($AnswerThree) && $AnswerThree =="F") echo "checked"; ?>
        value="F">False</p> . <?php "</td>";
        echo "</tr>\n";
        echo "</table>\n";?></p>
        <?php echo "<table border=\"1\"width=\"50%\">\n";
        echo "<tr>\n";
        echo "<td><p>Question 4 - True or False:</p>\n"; 
        echo  htmlentities($QuizQuestions[3]) . "\n";
        echo "<p>Answer: "?> <input type="radio" name="answerfour" <?php
        if (isset($AnswerFour) && $AnswerFour =="T") echo "checked"; ?> 
        value="T">True
        <input type="radio" name="answerfour" <?php
        if (isset($AnswerFour) && $AnswerFour =="F") echo "checked"; ?>
        value="F">False</p> . <?php "</td>";
        echo "</tr>\n";
        echo "</table>\n";?></p>
        <?php echo "<table border=\"1\"width=\"50%\">\n";
        echo "<tr>\n";

//表格将继续通过问题10       />

        </form>

        <?php // begins thefinal php code block
        }


// Here is the code that calls the get_question() function as well as the main program portion.

 get_questions();


    // checks to ensure propper user input has been entered    
    if (isset($_POST['Submit'])) {
        $User = validateInput($_POST['User'], "Your Name");
        $UserEmail = validateEmail($_POST['UserEmail'], "Your E-mail");
        $RevEmail = validateEmail($_POST['RevEmail'], "Reviewer E-mail");
        $Message = validateInput($_POST['Message'], "Message");
        $AnswerOne = validateInput($_POST['answerone'], "Answer One");
        $AnswerTwo = validateInput($_POST['answertwo'], "Answer Two");
        $AnswerThree = validateInput($_POST['answerthree'], "Answer Three");
        $AnswerFour = validateInput($_POST['answerfour'], "Answer Four");
        $AnswerFive = validateInput($_POST['answerfive'], "Answer Five");
        $AnswerSix = validateInput($_POST['answersix'], "Answer Six");
        $AnswerSeven = validateInput($_POST['answerseven'], "Answer Seven");
        $AnswerEight = validateInput($_POST['answereight'], "Answer Eight");
        $AnswerNine = validateInput($_POST['answernine'], "Answer Nine");
        $AnswerTen = validateInput($_POST['answerten'], "Answer Ten");

        if ($errorCount == 0) // doesnt reshow the form if input is valid
            $ShowForm = FALSE;
        }
    else
        $ShowForm = TRUE; // ensures form shows up in the first place

    if ($ShowForm == TRUE) {
        if ($errorCount>0) // if there were errors
            echo "<p>Please re-enter the form information below.</p>\n";
            displayForm($User, $UserEmail, $RevEmail, $Message, $QuizQuestions, $AnswerOne, $AnswerTwo, 
            $AnswerThree, $AnswerFour, $AnswerFive, $AnswerSix, $AnswerSeven, $AnswerEight, $AnswerNine, $AnswerTen, $QuizChoices, $QuizAnswers);
        }
    // finishes handling the quiz data and outputs results to user and emails results to both user and reviewer.    
    else  {
        $AnswerEight = ucfirst($AnswerEight);// we use ucfirst to ensure first letter of FIB answers input is uppercase which is how we put the answers
        $AnswerNine = ucfirst($AnswerNine);
        $AnswerTen = ucfirst($AnswerTen);

        // create an array containing the users answer input for use in grading.
        global $AnswerArray; 
        $AnswerArray = array($AnswerOne, $AnswerTwo, $AnswerThree, $AnswerFour, $AnswerFive, $AnswerSix, $AnswerSeven, $AnswerEight,
        $AnswerNine, $AnswerTen);

}

    ?> <!-- ends the final php code block-->

</body> <!-- ends the html body section.-->

 </html>

0 个答案:

没有答案