-2

I'm making an online quiz system using PHP, I've saved questions, their options and answers in a table in database. In the html page, I'm printing the questions and options through a while loop (options are radio buttons). I learned how to send radio button values to PHP, but the problem I'm facing is that, only the selected option for the first question is sent to PHP. Like example, if I've saved two questions in database and they are printed on the page using a while loop, now when if I select option1 for question1 and option2 for question2, and click on submit, this just posts the value option1 to PHP, and the others selected button values are lost. I want all these value to check through database if the answers are correct and then display score based on correct answers.

Here is the code of my html page, I'm posting the whole code, but I think you should just check the <form> tag.

<body>

<?php
include('connect.php');
$sql="Select * FROM html";
$record=mysql_query($sql);
?>      
<div id="container">
        <div class="header">
            <span class="heading"><span class="C">O</span>NLINE <span 
class="C">Q</span>UIZ <span class="C">S</span>YSTEM </span>
    <div class="logo"><img src="img/flogo.png" width="188" height="150" 
alt="logo"></div>

    </div>
        <div class="center">

         <nav>
            <ul>
            <li style="margin-right:180px; height:50px;width:auto"><a 
href="home.php">Username</a></li>
            <li><a href="Shome.php">Home</a></li>
            <li><a href="results.php">Results</a></li>
            <li><a href="welcome.php">Sign Out</a></li>
            </ul>
        </nav>
        <div id="page-wrap">
        <h1>HTML Quiz </h1>
            <form action="results.php" method="post" id="quiz">
            <ol>
            <li>
            <?php
            while($result=mysql_fetch_assoc($record))
            {
            echo "<form>";
            echo "<h3>";
            echo "".$result['id'].". ".$result['question']."";
            echo "</h3>";
            echo '<input type="radio" name="answer[]" id="question-1-answer-
A" value="A" />';
            echo '<label for="question-1-answer-A">';
            echo "".$result['option1']."";
            echo "</label>";
            echo '<input type="radio" name="answer[]" id="question-1-answer-
B" value="B" />';
            echo '<label for="question-1-answer-B">';
            echo "".$result['option2']."";
            echo "</label>";
            echo '<input type="radio" name="answer[]" id="question-1-answer-
C" value="C" />';
            echo '<label for="question-1-answer-C">';
            echo "".$result['option3']."";
            echo "</label>";
            echo '<input type="radio" name="answer[]" id="question-1-answer-
D" value="D" />';
            echo '<label for="question-1-answer-D">';
            echo "".$result['option4']."";
            echo "</label>";
            echo "</form>";
            }
            echo '<input type="Submit" value="Submit" name="submit" />';
            ?>
            </li>
            </ol>
            </form>

        </div>
    </div>

<div class="footer">
        <footer></footer> </div>
</div>
</body>

And the results.php

<?php
include('connect.php');
$sql="Select * FROM html";
$record=mysql_query($sql);
$result=mysql_fetch_assoc($record);
$total=0;
foreach($_POST['answer'] as $check) {
echo $check; 
}
echo "YOUR SCORE: ".$total;
?>

I know this results file does nothing right now, the foreach part will be used to check every answer with the answer in database and then total will be incremented, the code you see right now is just to check how many values are being sent to PHP. Currently I've printed 6 questions, and after selecting options for every question, when I click submit, it displays

AYOURSCORE: 0

Here 'A' being the value of the option selected in "Question 1", values for other questions are lost. Which is the main issue I'm facing.

I've to submit this project in 2 days, so any help will be greatly appreciated. Thanks

P.S. I am new to PHP` and have very little or no knowledge of java Script.

JJ.
  • 112
  • 2
  • 10
  • Would be helpful if you'd show us the code for inserting into the database and database's structure. – Vladut Apr 27 '17 at 12:55
  • 1
    ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 27 '17 at 13:01

2 Answers2

0

I think you are using two form. Where inside one form you repeating multiple for but in child form you have not provide action="". Or remove form from inside the loop.

<body>

<?php
include('connect.php');
$sql="Select * FROM html";
$record=mysql_query($sql);
?>      
<div id="container">
        <div class="header">
            <span class="heading"><span class="C">O</span>NLINE <span 
class="C">Q</span>UIZ <span class="C">S</span>YSTEM </span>
    <div class="logo"><img src="img/flogo.png" width="188" height="150" 
alt="logo"></div>

    </div>
        <div class="center">

         <nav>
            <ul>
            <li style="margin-right:180px; height:50px;width:auto"><a 
href="home.php">Username</a></li>
            <li><a href="Shome.php">Home</a></li>
            <li><a href="results.php">Results</a></li>
            <li><a href="welcome.php">Sign Out</a></li>
            </ul>
        </nav>
        <div id="page-wrap">
        <h1>HTML Quiz </h1>
            <form action="results.php" method="post" id="quiz">
            <ol>
            <li>
            <?php
            while($result=mysql_fetch_assoc($record))
            {
            echo "<h3>";
            echo "".$result['id'].". ".$result['question']."";
            echo "</h3>";
            echo '<input type="radio" name="answer[]" id="question-1-answer-
A" value="A" />';
            echo '<label for="question-1-answer-A">';
            echo "".$result['option1']."";
            echo "</label>";
            echo '<input type="radio" name="answer[]" id="question-1-answer-
B" value="B" />';
            echo '<label for="question-1-answer-B">';
            echo "".$result['option2']."";
            echo "</label>";
            echo '<input type="radio" name="answer[]" id="question-1-answer-
C" value="C" />';
            echo '<label for="question-1-answer-C">';
            echo "".$result['option3']."";
            echo "</label>";
            echo '<input type="radio" name="answer[]" id="question-1-answer-
D" value="D" />';
            echo '<label for="question-1-answer-D">';
            echo "".$result['option4']."";
            echo "</label>";
            }
            echo '<input type="Submit" value="Submit" name="submit" />';
            ?>
            </li>
            </ol>
            </form>

        </div>
    </div>

<div class="footer">
        <footer></footer> </div>
</div>
</body>
Sujeet Kumar
  • 159
  • 6
-1

I think, that the problem is in name of the input: name="answer[]". It is the same name for every radiobutton - the same for all questions. I think, that adding an index into brackets will help

name="answer[1]" for question one

name="answer[2]" for question two

and so on. The w3schools TryIt Editor lets you play with inputs.

  • The point of using `input type="radio"` is that you have multiple answers to the same question – meaning each field should have the ***same name*** with a ***different value***; having the same name forces you to only choose one answer. You must be confusing `radio` with `checkbox`? – Matt Nov 24 '21 at 07:00
  • @Matt I agree that the radiobutton should have the same name for all answers of **one** question. (With the checkboxes that is also true). However, int the _while_ loop the radiobuttons get printed, and in every loop they are the same. They are the same for **all** questions. There should be the variable $result['id'] in the name of those radiobuttons. Also, I am not sure if it does matter that the question is 4 years old? – Barbora Schramková Nov 24 '21 at 19:53