echo '<input class="form-check-input align" type="radio" value="1" name="answer'.$rownumber.' ">'.$one .'';
echo '<input class="form-check-input align " type="radio" value="2" name="answer'.$rownumber.' ">'. $two.'';
echo '<input class="form-check-input align" type="radio" value="3" name="answer'.$rownumber.' ">'. $three.'';
echo '<input class="form-check-input align" type="radio" value="4" name="answer'.$rownumber.' ">'. $four.'';?>
$answers[$question_id] gives you user choice to a question with id of $question_id. for storing in database, you can json_encode($answers) which converts all user's answers to an exam to string which is suitable for storing in a field in database.
the questions are coming from data.php, but for your usage, this is coming from database & each question is a table record i assume.
note that you don't have to echo the entire html tag in a foreach() loop. html snippets within closed php tags which determines the domain of a loop will be printed & displayed in user's browser.
you can open & close php tags as many times as you want(resulting in a cleaner file) & only print & work with parts of data within php tags which you have to process with php.
inputs of type radio & checkbox have strange behavior in such way that if they are unchecked, they won't pass on to the action script at all! so for each question we print 4 choices' radio inputs with same name attribute, resulting in:
only one of 4 could be selected
the answer & question are better related in $answers
<!-- html here --> <p><?php echo $MCQ['stem']; ?></p>
you have to tweak my _render.php file if you want to use it, because my naming doesn't match your field's name.
i hope it was helpful, sorry if it became long ;)
CodePudding user response:
echo '<input class="form-check-input align" type="radio" value="1" name="answer'.$rownumber.' ">'.$one;
echo '<input class="form-check-input align " type="radio" value="2" name="answer'.$rownumber.' ">'. $two;
echo '<input class="form-check-input align" type="radio" value="3" name="answer'.$rownumber.' ">'. $three;
echo '<input class="form-check-input align" type="radio" value="4" name="answer'.$rownumber.' ">'. $four;
first you have to remove the $rownumber from the name attribute i am writing the code below
echo '<input class="form-check-input align" type="radio" value="1" name="answer">'.$one;
echo '<input class="form-check-input align " type="radio" value="2" name="answer">'. $two;
echo '<input class="form-check-input align" type="radio" value="3" name="answer">'. $three;
echo '<input class="form-check-input align" type="radio" value="4" name="answer">'. $four;
then after form submit add the PHP code in PHP file
$answer = $_GET['answer'];
$sql = "INSERT INTO dbname(`answer`) VALUES('$answer');
$query = mysqli_query($conn, $sql);
if( $query ){
echo "SUCCESS";
}
else{
echo "FAILED";
}