1

I am a making quiz app it have a database table called question_bank it have columns :

id | question | a | b | c | d | right_ans | given_ans

I want to get all the questions from database and when user submit form it should compare given_ans with right_ans. I am able to get all the question from database but it is not functioning properly. It should select only one value from four given options but this code is allowing me to select all options. i need value of radio button to be a,b,c,d. because I will compare this with right_ans column.

    $query="SELECT * from question_bank";
    $result= mysqli_query($connection,$query);
    echo "<form method='post' action='exam.php'>";
    while ($read_all_data = mysqli_fetch_assoc($result))
    { 
    $id=$read_all_data['id'];
    $a=$read_all_data['a'];
    $b=$read_all_data['b'];
    $c=$$read_all_data['c'];
    $d=$read_all_data['d'];
     echo $read_all_data['question']."</br>";
     echo "A:<input type ='radio' value ='a'  name='$a' >".$a."</br>";
     echo "B:<input type ='radio' value ='b'  name='$b' >".$b."</br>";
     echo "C:<input type ='radio' value ='c'  name='$c' >".$c."</br>";
     echo "D:<input type ='radio' value ='d'  name='$d' >".$d."</br>";
    }
    echo "<input type='submit' value='submit' name='submit'>"[!

screenshot

hg8
  • 982
  • 1
  • 13
  • 27
eexam ilm
  • 145
  • 1
  • 1
  • 10

3 Answers3

1

Radio buttons have to have the same name in order for them to be mutually exclusive like you want:

 echo "A:<input type ='radio' value ='a'  name='response".$i."' >".$a."</br>";
 echo "B:<input type ='radio' value ='b'  name='response".$i."' >".$b."</br>";
 echo "C:<input type ='radio' value ='c'  name='response".$i."' >".$c."</br>";
 echo "D:<input type ='radio' value ='d'  name='response".$i."' >".$d."</br>";
sg-
  • 2,071
  • 1
  • 13
  • 15
  • if i give same name to the radio button it allow me to select only one option from all the questions. – eexam ilm Sep 22 '15 at 08:31
  • Also note that's not the right way to do a br tag. You can use
    or
    – sg- Sep 22 '15 at 08:37
  • sir, how can i get all the values back after submission of form? i want all the given ans back, to compare with right answers. – eexam ilm Sep 22 '15 at 09:49
  • You are asking a new question. Start a new question please and close this one off if the original question has been answered – sg- Sep 22 '15 at 10:12
0

You are executing in while loop. Then try this :

$i =1;

 while ($read_all_data = mysqli_fetch_assoc($result))
{ 
$id=$read_all_data['id'];
$a=$read_all_data['a'];
$b=$read_all_data['b'];
$c=$$read_all_data['c'];
$d=$read_all_data['d'];
 echo $read_all_data['question']."</br>";
 echo "A:<input type ='radio' value ='a'  name=response'$i' >".$a."</br>";
 echo "B:<input type ='radio' value ='b'  name=response'$i' >".$b."</br>";
 echo "C:<input type ='radio' value ='c'  name=response'$i' >".$c."</br>";
 echo "D:<input type ='radio' value ='d'  name=response'$i' >".$d."</br>";
 $i++;
}

It will make your radio name as response1, response 2 and so on...

Happy Coding
  • 2,433
  • 1
  • 8
  • 22
  • Sir, i need to ask one more thing. how would i get all the values of given answers when user submit the quiz? – eexam ilm Sep 22 '15 at 08:58
  • how would i get all the values of given answers when user submit the quiz? i am doing if(isset($_POST['submit'])) { $a=$_POST['response.$i']; } – eexam ilm Sep 22 '15 at 09:05
  • From where you are getting $i after POST – Happy Coding Sep 22 '15 at 09:13
  • i am trying to get values of questions from my form.and after submission i want to get those values back to compare with rights answers. – eexam ilm Sep 22 '15 at 09:20
0

Building the form.

Radio buttons are usually grouped together into groups, radio buttons that share the same name attribute are part of one group ( all answers to one question in example ). Since none of the answers are checked by default, when neither is checked in the group of radio buttons that input will not be submitted. To prevent that use required attribute on first radio button in the group, that will tell the browser to submit the form only if the input is not empty.

Here is an example, i'm using mysqli in object oriented style:

$connection = new mysqli( 'host', 'user', 'pass', 'db' );

$questions = $connection->query( "SELECT * FROM question_bank" );

echo "<form method='POST' action='exam.php'>";
while ( $row = $questions->fetch_assoc() ) {
    echo $row[ 'question' ] . "<br>";
    echo "<input type ='radio' value ='a'  name='$row[id]' required>" . $row[ 'a' ] . "<br>";
    echo "<input type ='radio' value ='b'  name='$row[id]' >" . $row[ 'b' ] . "<br>";
    echo "<input type ='radio' value ='c'  name='$row[id]' >" . $row[ 'c' ] . "<br>";
    echo "<input type ='radio' value ='d'  name='$row[id]' >" . $row[ 'd' ] . "<br>";
}
echo "<input type='submit' value='submit' name='submit'></form>";


Handling the form request.

Check if the form is submitted via post method with !empty( $_POST['submit'] ) ( or by using $_SERVER['REQUEST_METHOD'] === 'POST' ), an loop through all the questions to compare the received answer with right answer. Since radio buttons are grouped using post id as the name attribute, $_POST array will look like:

Array
(
    [1] => b
    [2] => c
    [3] => c
    [submit] => submit
)

, where the key is id of the question, and the value is selected answer by user.

Example of how to validate the form:

$connection = new mysqli( 'host', 'user', 'pass', 'db' );

if ( !empty( $_POST['submit'] ) ) {
    $questions = $connection->query( "SELECT * FROM question_bank" );

    while ( $row = $questions->fetch_assoc() ) {

        if ( !empty( $_POST[ $row[ 'id' ] ] ) ) {           
            $ans = $row[ $_POST[ $row[ 'id' ] ] ];

            if ( $ans === $row[ 'right_ans' ] ) {
                echo $row[ 'question' ] . ': correct<br>';
            } else {
                echo $row[ 'question' ] . ': incorrect<br>';
            }
        } else {
            echo $row[ 'question' ] . ': no answer<br>';
        }       
    }   
}

How to compare the given answers with right ones in the form.

Ok, today i'm in a good mood so here is a revised example, you can save the provided answers in an array and use that array when building the form. I'm also showing you how to check the right answer after form submission by using ternary operator ( ( $checked && 'd' == $checked ? 'checked' : '' ) )

$answers = [];
$questions = $connection->query( "SELECT * FROM question_bank" );

if ( !empty( $_POST['submit'] ) ) {

    while ( $row = $questions->fetch_assoc() ) {

        if ( !empty( $_POST[ $row[ 'id' ] ] ) ) {           
            $ans = $row[ $_POST[ $row[ 'id' ] ] ];

            if ( $ans === $row[ 'right_ans' ] ) {
                $answers[ $row[ 'id' ] ] = "'$ans' is correct";
            } else {
                $answers[ $row[ 'id' ] ] = "'$ans' is not correct, correct is $row[right_ans]";
            }
        } else {
            $answers[ $row[ 'id' ] ] = 'No answer provided';
        }

    }

}

$questions = $connection->query( "SELECT * FROM question_bank" );

echo "<form method='POST' action='exam.php'>";
while ( $row = $questions->fetch_assoc() ) {

    echo $row[ 'question' ] . "<br>";

    $checked = null;
    if ( isset( $answers[ $row[ 'id' ] ] ) ) {
        echo $answers[ $row[ 'id' ] ] . "<br>";
        $checked = array_search( $row[ 'right_ans' ], array( 'a' => $row[ 'a' ], 'b' =>  $row[ 'b' ], 'c' =>  $row[ 'c' ], 'd' =>  $row[ 'd' ] ) );
    }   

    echo "<input type ='radio' value ='a'  name='$row[id]' required " . ( $checked && 'a' == $checked ? 'checked' : '' ) . ">" . $row[ 'a' ] . "<br>";
    echo "<input type ='radio' value ='b'  name='$row[id]' " . ( $checked && 'b' == $checked ? 'checked' : '' ) . ">" . $row[ 'b' ] . "<br>";
    echo "<input type ='radio' value ='c'  name='$row[id]' " . ( $checked && 'c' == $checked ? 'checked' : '' ) . ">" . $row[ 'c' ] . "<br>";
    echo "<input type ='radio' value ='d'  name='$row[id]' " . ( $checked && 'd' == $checked ? 'checked' : '' ) . ">" . $row[ 'd' ] . "<br>";

}
echo "<input type='submit' value='submit' name='submit'></form>";
Community
  • 1
  • 1
Danijel
  • 11,490
  • 4
  • 32
  • 51