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>";
or
– sg- Sep 22 '15 at 08:37