从POST回应变量

时间:2014-10-13 17:45:50

标签: php

我已经尝试过这样做了..

 <?php

        $A1 = $_POST['question-1-answers'];
        $A2 = $_POST['question-2-answers'];
        $A3 = $_POST['question-3-answers'];
        $answer1 = $_POST['question-1-answers'];
        $answer2 = $_POST['question-2-answers'];
        $answer3 = $_POST['question-3-answers'];

        $totalCorrect = 0;

        if ($answer1 == "X") { $totalCorrect++; }
        if ($answer2 == "X") { $totalCorrect++; }
        if ($answer3 == "X") { $totalCorrect++; }
        if ($answer5) { $totalCorrect++; }

        echo "Your answer for question 1 was: $A1<br> ";
        echo "Your answer for question 2 was: $answer2 <br>";
        echo "Your answer for question 3 was: $answer3 <br>";
        echo "<div id='results'>$totalCorrect / 3 correct</div>";

    ?>

但它似乎不起作用。我想要实现的是答案是目前的一个固定变量(100%工作为$ answer1 / 2/3但不确定为$ A1 / 2/3)。然后告诉他们他们回答了什么。关于如何做到这一点的任何想法。我知道POST部分有效,因为如果答案是正确的,那么总的正确计数会上升。有任何想法吗。我试过摆弄并添加$ A1变量等。

表格代码:

<form action="##.php" method="post" id="quiz">

        <ol>

            <li>

                <h3>XXX</h3>

                <div>
                    <label for="question-1-answers-A">Answer:</label>
                    <input type="text" name="question-1-answers" id="question-1-answers-A" placeholder="XXX" />
                </div>

            </li>

            <li>

                <h3>XXXX</h3>

                <div>
                    <label for="question-2-answers-A" id="question-2-answers-A">Answer:</label>
                    <select name="iPhone" id="question-2-answers" name="question-2-answers">
            <option id="A" value="A">XXX</option>
            <option id="B" value="B">XXX</option>
            <option id="C" value="C">XXX</option>
            <option id="D" value="D">XXX</option>
        </select>
                </div>


            </li>

            <li>

                <h3>XXX</h3>

                <div>
                    <input type="radio" name="question-3-answers" id="question-3-answers-A" value="A" />
                    <label for="question-3-answers-A">A) X</label>
                </div>

                <div>
                    <input type="radio" name="question-3-answers" id="question-3-answers-B" value="B" />
                    <label for="question-3-answers-B">B) X</label>
                </div>

                <div>
                    <input type="radio" name="question-3-answers" id="question-3-answers-C" value="C" />
                    <label for="question-3-answers-C">C) X</label>
                </div>

                <div>
                    <input type="radio" name="question-3-answers" id="question-3-answers-D" value="D" />
                    <label for="question-3-answers-D">D) X</label>
                </div>

            </li>
         </div>
         <button id="myButton" class="float-center submit-button" onClick="setCookie()" >Next Page</button>

2 个答案:

答案 0 :(得分:0)

这是我的工作代码。您可以使用您的quizz值来调整它。 这里的正确答案是X A C:

PHP:

<?php

$answer1 = $_POST['question-1-answers'];
$answer2 = $_POST['question-2-answers'];
$answer3 = $_POST['question-3-answers'];

$totalCorrect = 0;

if ($answer1 == "X") {
    $totalCorrect++;
}
if ($answer2 == "A") {
    $totalCorrect++;
}
if ($answer3 == "C") {
    $totalCorrect++;
}
if ($answer5) {
    $totalCorrect++;
}

echo "Your answer for question 1 was: $answer1<br> ";
echo "Your answer for question 2 was: $answer2 <br>";
echo "Your answer for question 3 was: $answer3 <br>";
echo "<div id='results'>$totalCorrect / 3 correct</div>";

?>

HTML

<form action="##.php" method="post" id="quiz">

    <ol>

        <li>

            <h3>XXX</h3>

            <div>
                <label for="question-1-answers-A">Answer:</label>
                <input type="text" name="question-1-answers"
                       id="question-1-answers-A" placeholder="XXX"/>
            </div>

        </li>

        <li>

            <h3>XXXX</h3>

            <div>
                <label for="question-2-answers-A" id="question-2-answers-A">Answer:</label>
                <select id="question-2-answers"
                        name="question-2-answers">
                    <option id="A" value="A">A</option>
                    <option id="B" value="B">B</option>
                    <option id="C" value="C">C</option>
                    <option id="D" value="D">D</option>
                </select>
            </div>


        </li>

        <li>

            <h3>XXX</h3>

            <div>
                <input type="radio" name="question-3-answers"
                       id="question-3-answers-A" value="A"/>
                <label for="question-3-answers-A">A) X</label>
            </div>

            <div>
                <input type="radio" name="question-3-answers"
                       id="question-3-answers-B" value="B"/>
                <label for="question-3-answers-B">B) X</label>
            </div>

            <div>
                <input type="radio" name="question-3-answers"
                       id="question-3-answers-C" value="C"/>
                <label for="question-3-answers-C">C) X</label>
            </div>

            <div>
                <input type="radio" name="question-3-answers"
                       id="question-3-answers-D" value="D"/>
                <label for="question-3-answers-D">D) X</label>
            </div>

        </li>
        </div>
        <button id="myButton" class="float-center submit-button"
                onClick="setCookie()">Next Page
        </button>
    </ol>
</form>

答案 1 :(得分:0)

看看这是否适合您,

$answer1 = $_POST['question-1-answers'];
$answer2 = $_POST['question-2-answers'];
$answer3 = $_POST['question-3-answers'];

$totalCorrect = 0;

if ($answer1 == "X") { $totalCorrect++; }
if ($answer2 == "X") { $totalCorrect++; }
if ($answer3 == "X") { $totalCorrect++; }
if ($answer5) { $totalCorrect++; }

echo "Your answer for question 1 was:". $answer1 ."<br> ";
echo "Your answer for question 2 was:". $answer2 ."<br>";
echo "Your answer for question 3 was:". $answer3." <br>";
echo "<div id='results'>".$totalCorrect." / 3 correct</div>";

无需分配$ A1,$ A2,$ A3,因为您为$ answer1,$ answer2,$ answer3分配了相同的值。所以它要么使用变量$ A1,$ A2,$ A3或$ answer1,$ answer2,$ answer3,只要确保你相应地改变其他变量,这样你的计算就不会被打破。

需要考虑的事项:

  • 确保在从帖子
  • 中提取值时使用正确的字段名称
  • 您显示的代码容易受到XSS攻击。请尝试考虑改进您的代码。这可能对您有用[{3}}