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.