Why are my radio buttons overlapping? - php

I'm trying to make a quiz form which displays questions and 3 answers which are taken from my database. When I run my loop, it displays all the questions and answer choices correctly with the radio button but when I click on question 1 answer "b" then click question 2 answer "c" the radio button un-clicks question 1's answer "b" and highlight question 2 answer "c";
Answering question 1 Trying to answer question 2 after answering question 1
1. What is your name? 1. What is you name?
()John ()John
(*)Jake ()Jake <--un-clicked
()Joe ()Joe
2. Where are you from? 2. Where are you from?
()San Antonio ()San Antonio
()Austin ()Austin
()New York (*)New York
If you notice question 1 answer is removed when question 2 is answered.
Here is my code that retrieves the data from the database and displays it.
$mysql = "SELECT * FROM $table WHERE $table.quiz_name = '$name'";
$mydata = mysql_query($mysql,$con);
//post quiz name (here)
echo $name."</br>";
while($records = mysql_fetch_array($mydata)){
echo "<div>";
echo $records['question_description']."<br>";
//image displayed here
echo "<label><input type='radio' name='option' value=".$records['option_a'].">".$records['option_a']."</label><br>";
echo "<label><input type='radio' name='option' value=".$records['option_b'].">".$records['option_b']."</lable><br>";
echo "<label><input type='radio' name='option' value=".$records['option_c'].">".$records['option_c']."</label><br>";
echo "</ br> <hr>";
echo "</div>";
}
I hope someone can help me solve this issue.

you are grouping all radio buttons with 'option'... do this;
$counter = 0 //declare a counter outside the loop
and add this with the name
name='option".$counter."'
and increment counter inside the loop
$counter++
This will give you a group for each set of radio buttons

Your radio buttons need different names. Having them all with the same name will mean you can only select one out of every radio you have.
Your radio buttons should have a different name for each question. Consider using something like your database ID like so:
while($records = mysql_fetch_array($mydata)) {
echo $records['question_description']."<br>";
echo "<label><input type='radio' name='option" . $record['id'] . "' value=".$records['option_a'].">".$records['option_a']."</label><br>";
echo "<label><input type='radio' name='option" . $record['id'] . "' value=".$records['option_b'].">".$records['option_b']."</lable><br>";
echo "<label><input type='radio' name='option" . $record['id'] . "' value=".$records['option_c'].">".$records['option_c']."</label><br>";
}

Related

Continue to next page when filling in right value

I'm making a quiz for my students. I would like to make it in such a way that you can only see question 2 after you solved question 1 (and so on).
My current idea is to make a web page for every question with a form on it:
<FORM action="test.php" method="post">
<I>12 is the right answer:</I>
<INPUT TYPE="text" NAME="name" SIZE="20" MAXLENGTH="30"><BR>
<INPUT TYPE="submit" VALUE="send">
</FORM>
And afterwards, I try to redirect from test.php to next.php whenever the answer is 12 and to current.php when the answer is not 12. Though, I am not able to make this work. Can anyone help me?
Have not written php code for a while now but will try to guide you.
No, you should not create a web page for each question. That is not a scalable approach. Imagine if you have to store 1000 questions over time,period.
Instead use dynamic page loading concept in php. Here what you should be doing:
1.Create a table in whichever database you are using with fields like
Id,question,option1,option2,option3,option4,correctAnswer
Create a php page like quiz.php which reads the question based on questionId stored in the session variable.
Lets say you show the first question in front page, displad the options and showed a submit button.
When the user clicks submit button what should happen is that the same quiz.php page will get called courtsy <form action="quiz.php"> with the user selected answer.
You can then check for the correct answer in php file since you have the correct answer stored in a variable which stores the database fetched result of first question, and if that is correct answer you can update the session with id of next question (increment by 1 or any other mechanism) and query the database table for that question id .
Your html should be written in such a way away which reads the current value stored in $row variable, which stores the result of the query w.r.t to questionId stored in session.
little bit of the code :
quiz.php file:
<?php
$con=mysql_connect($dbserver,$dbusername,$dbpassword);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($dbname, $con);
// Check user answer for previous question
if (isset($_POST['submit'])) {
if(isset($_SESSION['question_id'])){
$previous_question_id = (int) $_POST['question_id'];
// Query database
$getQuestion = "SELECT * from questions_table where id =
$previous_question_id";
$resultOfQuestion = mysqli_query($conn, $getQuestion);
$correct = $row_get_question['correctAnswer'];
$selected_radio = $_POST['answer'];
if ($selected_radio == $correct)
echo "THAT ANSWER IS CORRECT";
$_SESSION['question_id'] = $previous_question_id +1;
$getQuestion = "SELECT * from questions_table where
(id=".$_SESSION['question_id'].")";
$resultOfQuestion = mysqli_query($conn, $getQuestion);
else
echo "THAT ANSWER IS WRONG!";
}
}
if(!isset($_SESSION['question_id'])) {
$_SESSION['question_id']="0";
$getQuestion = mysql_query("SELECT * FROM questions WHERE
(id=".$_SESSION['question_id'].")";
$resultOfQuestion = mysqli_query($conn, $getQuestion);
}
echo "<form action='quiz.php' method='post'>";
while($row = mysql_fetch_array($resultOfQuestion)){
echo " <b>" . $row['question'] . "</b>";
echo "<input type='radio' value='' name='answer' checked />";
echo $row['option1']; echo "<br />";
echo "<input type='radio' value='' name='answer' />";
echo $row['option2'];echo "<br />";
echo "<input type='radio' value='' name='answer' />";
echo $row['option3'];echo "<br />";
echo "<input type='radio' value='' name='answer' />";
echo $row['option4'];echo "<br />";
echo "<input type='submit' name='next' value='next' />";
}

How to distribute the results retrieved from database into individual pages?

I'm facing such a problem,
I have a database bank with lots of questions and their joined answers 'multiple choice system'!
I wrote a PHP script to retrieve (15) questions each time a user enters the page, 'index.php' as a radio-buttons.
Now my bass asked me to change the method of showing the questions! so, instead of 15 questions at the same page! he asked to show one question per time, something like this:
================================================================================
1. Question TEXT:
a. 1st variant
b. 2nd variant
c. 3rd variant
d. 4th variant
<NEXT QUESTION>
================================================================================
so by clicking on the button which is a "submit" the script should redirect me to the second part of the file which must show the second question, and from the second as shown above to the third --> forth --> ... --> 15th question
================================================================================
1st question is: Correct //This part will check the correction/incorrection of the previous question -_-
2. Question2 TEXT:
a. 1st variant
b. 2nd variant
c. 3rd variant
d. 4th variant
<NEXT QUESTION>
================================================================================
bottom line, I am able to bring a 15 question on the page and after checking the answers I give the results. but I could figure out how to distribute these questions into individual pages!! as
'index.php?question=1'
'index.php?question=2'
'index.php?question=3'
...
'index.php?question=15'
Thank you guys!
so as I said, I wanted to distribute the results retrieved from one query into individual pages, and I came up with this idea,
1. Did the same query
SELECT * FROM database ORDER BY RAND() LIMIT 10
then I saved the IDz of the retrieved questions into an array just like that
$id = array();
$_SESSION[corr] = 0;
$_SESSION[incorr] = 0;
while ($row = mysql_fetch_assoc($query) {
$id[] = $row[ID];
}
Then I saved the array into a SESSION to pass it in every required page as follows
$_SESSION[ID] = $id;
after that I set a link such as
echo "<a href=test.php?question=1>Click To Start</a>";
and last but not least did something like this
if ($_REQUEST['question'] == '1') {
$id = $_SESSION['id'];
$sql = mysql_query("SELECT * FROM `test` WHERE `id` = '$id[0]'") or die("Error" .mysql_error());
$row = mysql_fetch_assoc($sql);
echo "<font color=green>Correct: " .$_SESSION[corr]. "</font></br>";
echo "<font color=red>Incorrect: " .$_SESSION[incorr]. "</font></br>";
echo "$row[v]<br>";
echo "<form action='test.php?question=2' method='POST'>";
echo "<input type=radio name=answer1 value=1>$row[o1]</input><br>";
echo "<input type=radio name=answer1 value=2>$row[o2]</input><br>";
echo "<input type=radio name=answer1 value=3>$row[o3]</input><br>";
echo "<input type=radio name=answer1 value=4>$row[o4]</input><br>";
echo "<input type=submit name=submit value=Check>";
echo "</form>";
$_SESSION['previous_id'] = $row['id'];
exit;
and in the next part "test.php?question=2"
I firstly checked if the previous question was answered correctly as the following
if ($_REQUEST['question'] == '2') {
$id = $_SESSION['id'];
$answer = $_POST['answer1'];
$grade = mysql_query("SELECT * FROM `test` WHERE `id` = {$_SESSION['previous_id']}") or die();
$right = mysql_fetch_assoc($grade);
if ($answer == $right[g]) {
$_SESSION['corr']++;
echo "<font color=green>Correct</font></br>";
}
else {
$_SESSION['incorr']++;
echo "<font color=red>Incorrect, check lesson #".$right[lvl]."</font></br>";
}
$sql = mysql_query("SELECT * FROM `test` WHERE `id` = '$id[1]'") or die("Error" .mysql_error());
$row = mysql_fetch_assoc($sql);
echo "$row[v]<br>";
echo "<form action='test.php?question=3' method='POST'>";
echo "<input type=radio name=answer2 value=1>$row[o1]</input><br>";
echo "<input type=radio name=answer2 value=2>$row[o2]</input><br>";
echo "<input type=radio name=answer2 value=3>$row[o3]</input><br>";
echo "<input type=radio name=answer2 value=4>$row[o4]</input><br>";
echo "<input type=submit name=submit value=Check>";
echo "</form>";
$_SESSION['previous_id'] = $row['id'];
exit;
}
That worked just 100% as I wanted, but it is just not feeling professional, because I had to rewrite that code, or copy and edit the same above shown codes lots of times, considering I am trying to post 10 or 15 questions separately..
hopefully that helps some one else!

how to get php counter contents associated with variables

I have written a quiz script and it all works fine. What I want to do now is some fine tuning.
There are 6 questions. The questions and answers are defined in variables like this:
$q7="(7) __________ you like the red one or the blue one? (Positive, simple present)";
$a7="(7) <strong>Do</strong> you like the red one or the blue one? (Positive, simple present)";
As the person answers each question there is a counter that increments each correct answer.
I can view the counter and see that it contains the correct number of responses, but it is only a number.
If the person gets 3 out of 6 correct I show them the score like this -
You got 3/6 correct.
What I want to do is also show them the 3 correct answers they chose, and not the incorrect answers.
Is this possible? If the counter knows there are 3 correct answers, how do I get that info? Those particular variables?
How do I get this -
if $counter contains 4 or less correct answers then display those answers.
// *** question 12 *** //
print "<p> </p><table id='english_nb'><tr>";
print "<th>$q12</th></tr></table>";
print "<table id='english_nb'><tr>";
if ($_POST['answer12']==$do)
print "<td width='25%'><input type='radio' checked='checked' name='answer12' value='$do' />$do</td>";
else
print "<td width='25%'><input type='radio' name='answer12' value='$do' />$do</td>";
if ($_POST['answer12']==$didnt){
print "<td width='25%'><input type='radio' checked='checked' name='answer12' value='$didnt' />$didnt</td>";
$correct++;
}
else
print "<td width='25%'><input type='radio' name='answer12' value='$didnt' />$didnt</td>";
if ($_POST['answer12']==$doesnt)
print "<td width='25%'><input type='radio' checked='checked' name='answer12' value='$doesnt' />$doesnt</td>";
else
print "<td width='25%'><input type='radio' name='answer12' value='$doesnt' />$doesnt</td>";
if ($_POST['answer12']==$did)
print "<td width='25%'><input type='radio' checked='checked' name='answer12' value='$did' />$did</td>";
else
print "<td width='25%'><input type='radio' name='answer12' value='$did' />$did</td>";
print "</tr></table>";
// *** check the answers *** //
foreach ($_POST as $value){
if (isset ($value)){
$done++;
}
}
if ($done !=7) //set this to 1 higher than the number of questions and answers
print "<input type='submit' name='submit' value='Check answers' />";
if (($correct<5)&&($done==7)) //set this to the number of minimum correct answers
print "<p>You should review the information and try the quiz again.</p>";
print "<p>Your score is $correct/6 correct.</p>"; //set this to the same number of questions and answers
if (($done > 0)&&($done < 7)) //set this to 1 higher than the number of questions and answers
print "<p>You haven't answered all the questions. Please finish the quiz and re-submit your answers.</p>";
if(($done==7)&&($correct>4))
{ //set this to 1 higher than the number of questions and answers
print "<p>Your score is $correct/6 correct.</p>"; //set this to the same number of questions and answers
if ($correct==0)
$correct='0';
else
{
print "<p>The correct answers:</p>";
print "<p>$a7</p>";
print "<p>$a8</p>";
print "<p>$a9</p>";
print "<p>$a10</p>";
print "<p>$a11</p>";
print "<p>$a12</p>";
You could improve your code a lot, arrays would be a great start as others have said.
A simple way to do what you want could be adding a $correctAnswers = ""; variable, and everytime you use $correct++; add $correctAnswers .= "<p>$a1</p>"; and so on.
Then you could show them with print $correctAnswers;
$q=array($q1,$q2,....); //questions array
$a=array($a1,$a2,....); //answers array
correctAnswer=array(); //array of correct answers. empty at start
if (question=$q[0] && answer=$a[0]){ //if question and answer match
$correctAnswer[]=$a[0]; //adds the correct answer to the array
}
else{
// do something if the q &a doesnt match
}
$correct_answer_count = count($correctAnswer);

checkbox appears dynamic in php

Here is my piece of code:
$a=mysql_query("SELECT denumire_intrebare,denumire_varianta,tip_intrebare
FROM intrebari,variante
WHERE intrebari.cod_chestionar='".$_SESSION['cod']."'
AND intrebari.cod_intrebare=variante.cod_intrebare");
while($b=mysql_fetch_array($a))
if($b['tip_intrebare']==3)
{
echo $b['denumire_intrebare'];
echo "<br>";
echo "<input type='checkbox' name='option1' value='Milk'>";
echo $b['denumire_varianta'];
echo "<br>";
}
So let me explain. I query the database and it brings me up a question and the answers that are related to it. I want the answers to put them as checkbox answers. The problem is that my question is repeating for all the answers. So if i have 5 answers then the question appears 5 times like this: question answer1, question answer2, ... . I want my question to appear only once with the answers under the question. I'm missing something but I don't know what. Any help?
Make an array with all $b['denumire_varianta'] and use a foreach() after the while has finished, also make your checkboxes array so you can get them afterwards:
$variante = array();
while($b=mysql_fetch_array($a)) {
if($b['tip_intrebare']==3){
$intrebare = $b['denumire_intrebare'];
$variante[] = $b['denumire_varianta'];
}
}
echo $intrebare."<br />";
foreach($variante as $varianta){
echo "<input type='checkbox' name='option[]' value='".$varianta."'>";
echo $varianta."<br />";
}

Pulling a survey with multiple answers from a database

I have a working survey that pulls each question and any related answers from multiple tables. Each question is stored in the following table:
tblQuestions, with the fields:
qID
qText.
The related answers are stored in the table:
tblPossAnswers, with the fields:
aID
qID
answerText.
So, I would have 3 possible answers for each question. My sql to pull everything is:
select * from tblQuestions, tblPossAnswers where
tblPossAnswers.qID = tblQuestions.qID
order by tblQuestions.qID ASC
And my PHP to display it:
while ($row = mysql_fetch_array($result)) {
echo "<p>" . $row['qText'] . "</p><br />";
echo "<input type='radio' name='".$row['qID']."' value='".$row['aID']."' />";
echo $row['answerText'];
}
The problem is this is displaying the qText every time it displays a possible answer. So it looks like:
Question 1
Possible answer 1
Question 1
Possible answer 2
Question 1
Possible answer 3
Question 2
Possible answer 1
Question 2
Possible answer 2
Question 2
Possible answer 3
What I would like to do is have the qText only display when the first possible answer is pulled. I'm still somewhat of a newb to MySQL, so the solution might be something very simple that I'm just not seeing.
You can either test for whether the question has changed within your PHP loop:
while ($row = mysql_fetch_array($result)) {
if ($row['qID'] != $lastQuestionID) {
echo "<p>" . $row['qText'] . "</p><br />";
$lastQuestionID = $row['qID'];
}
echo "<input type='radio' name='".$row['qID']."' value='".$row['aID']."' />";
echo $row['answerText'];
}
Or else you can group the MySQL results by question using GROUP_CONCAT, specifying a separator on which you then split the answers in PHP:
select *, group_concat(answerText separator char(30)) as answers
from tblQuestions join tblPossAnswers using (qID)
group by tblQuestions.qID
order by tblQuestions.qID ASC
Then:
while ($row = mysql_fetch_array($result)) {
echo "<p>" . $row['qText'] . "</p><br />";
foreach (explode(chr(30), $row['answers']) as $answer) {
echo "<input type='radio' name='".$row['qID']."' value='".$answer."' />";
echo $answer;
}
}
No need to change anything in sql just change php slightly as following
while ($row = mysql_fetch_array($result)) {
if ($row['qText'] != $lastQuestion) {
echo "<p>" . $row['qText'] . "</p><br />";
$lastQuestion = $row['qText'];
}
echo "<input type='radio' name='".$row['qID']."' value='".$row['aID']."' />".$row['answerText'];
}

Categories