-1

I have already tried doing this..

 <?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>";

    ?>

But it does not seem to work. What I am trying to achieve is that the answer is a set variable at the moment (100% working as $answer1/2/3 but not sure as $A1/2/3). To then show them what they answered. Any ideas on how to do this. I know the POST part works because if the answer is correct, the total correct tally goes up. Any ideas. I have tried fiddling and adding the $A1 variables etc.

Code with form:

<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>
Nick
  • 1
  • 3
  • 3
    Add error reporting to the top of your file(s) right after your opening ` – Funk Forty Niner Oct 13 '14 at 17:46
  • The form answers are submitted to that page. I get an error adding that code. @peter – Nick Oct 13 '14 at 17:51
  • 1
    I'd sure like to know what that error is ;) as it stands, your question is unclear. I'd have to totally rebuild your code and I'm not up to doing that, I can only outline the obvious, which another is missing named elements. Is this your full code? If it isn't, show it. – Funk Forty Niner Oct 13 '14 at 17:53
  • Can you show us the HTML of your quizz form ? – Alban Pommeret Oct 13 '14 at 17:55
  • Your variables $A1 = $answer1, $A2 = $answer2, $A3= $answer3, its Ambiguous. Your question is unclear, I've read it more and more but I couldn't really understand. If you can rephrase it, it would be better. – user1149244 Oct 13 '14 at 17:56
  • @peter I have updated my source code... What I am trying to do is echo the variable which is $answer1 etc.. – Nick Oct 13 '14 at 18:01
  • Errr. who is Peter? lol – Funk Forty Niner Oct 13 '14 at 18:04
  • **Danger**: This code is [vulnerable to XSS](https://www.owasp.org/index.php/Cross-site_Scripting_(XSS)). User input needs escaping before being inserted into an HTML document!. – Quentin Oct 13 '14 at 18:06
  • try concatenating instead and see if that changes anything. Putting the variable inside the quotes didn't always work for me. So do: echo("My variable" . $answer1 ."
    ");
    – broch Oct 13 '14 at 18:06
  • Your HTML is invalid in ways that will break your form. Use a [validator](http://validator.w3.org). – Quentin Oct 13 '14 at 18:07
  • I modified a bit of your code, is this what you expect? http://screencast.com/t/EenVelhAle – user1149244 Oct 13 '14 at 18:15
  • Your error comes from your two name attributes in the select also. Remove the `name="iPhone"` one – Alban Pommeret Oct 13 '14 at 18:20

2 Answers2

0

Here is a working code for me. You can adapt it using the values for your quizz. The correct answer here is 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>
Alban Pommeret
  • 327
  • 1
  • 10
  • I am have 3 pages, they go to a answerpage1.php.. answerpage2.php etc.. after each Page1.php.. Page2.php.. etc.. Then at the end i use the include(); function of each page.. therefore it shouldn't matter. I use a redirect on the cpanel to redirect each answerpage.php to the next Page2.php..? I hope that makes it clearer.. But it shouldn't matter. After all I am only include(); so its what ever on the html and answerpage.php files. – Nick Oct 13 '14 at 19:25
  • I'm sorry but to be honnest, it makes it more vague :). Can you try to make a php file (ex: "quizz.php"), add all the code above in it, (the HTML goes just after the PHP) code and try it ? – Alban Pommeret Oct 13 '14 at 19:33
  • But the structure of the quiz needs to be: x many pages, with x many questions on each then the answer page. – Nick Oct 13 '14 at 19:41
  • Do you know that the POST will not be persisted between each pages ? You need to store your answers in $_SESSION if you have multiple pages. – Alban Pommeret Oct 13 '14 at 19:45
  • After each page, you have to catch the `$_POST` and store it in `$_SESSION` like that : `$_SESSION['question-1-answers'] = $_POST['question-1-answers'];` and then you read your answers from `$_SESSION`. Don't forget to set `session_start()` at the very beginning of your php scripts. Here is some documentation : http://php.net/manual/fr/session.examples.basic.php – Alban Pommeret Oct 13 '14 at 19:56
  • You have to use concatenation when you're echoing a value of an array, you can't use `$` in `""`. Ex: `echo "Your answer for question 1 was:" . $_SESSION['question-1-answers'];`. You also have to add a semicolon at the end of the session_start() line : `session_start();` – Alban Pommeret Oct 13 '14 at 21:02
  • Should this then display then answer? @albanpommeret – Nick Oct 14 '14 at 07:19
  • When I add this code: session_start() $_SESSION['question-1-answers'] = $_POST['question-1-answers']; $_SESSION['question-2-answers'] = $_POST['question-2-answers']; $_SESSION['question-3-answers'] = $_POST['question-3-answers']; I get an error code of Parse error: syntax error, unexpected ':' in /home/xx/public_html/Quiz/answerpage1.php on line 23 @albanpommeret any ideas? – Nick Oct 14 '14 at 12:01
  • All I get know is these two errors: Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/xx/public_html/Quiz/finalanswerpage.php:20) in /home/devstopco/public_html/Quiz/answerpage1.php on line 21 Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/xxx/public_html/Quiz/finalanswerpage.php:20) in /home/devstopco/public_html/Quiz/answerpage1.php on line 21 The line 21 is: session_start(); @albanpommeret – Nick Oct 14 '14 at 18:47
  • The session_start has to be the first line of your file. You must not send any content before it. I'm sorry but I think you need to learn a bit more about php before working on a project like that. I can help you of course but I'm afraid it seems to be a little bit annoying... – Alban Pommeret Oct 14 '14 at 19:26
0

See if this works for you,

$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>";

No need to assign for $A1, $A2, $A3 because you are assigning the same values for $answer1, $answer2, $answer3. So its either you will use variable $A1, $A2 , $A3 or $answer1, $answer2, $answer3, just make sure you will change the other variables accordingly so that your computation wont be broken.

Things to Consider:

  • Make sure that you are using the right field name when extracting values from post
  • The code that you show is vulnerable to XSS attack. Please try considering of improving your code. This might be helpful for you Best practices for avoiding XSS attack
Community
  • 1
  • 1
user1149244
  • 711
  • 4
  • 10
  • 27