I need help with my PHP page where I display multiple choice questions from a database along with options and answers. Currently, I am using an HTML button to show/hide the answers but it only works for the first question. When I click on other questions, it still shows the answer for the first question. Can anyone assist me with this issue?
<script>
function toggleAnswer() {
var x = document.getElementById('myDIV');
if (x.style.display === 'none') {
x.style.display = 'block';
} else {
x.style.display = 'none';
}
}
</script>
<?php
while($row = mysqli_fetch_array($result)) {
echo "<h4 style=font-weight:bold; text-align:justify>";
echo "Q $row[slno]) $row[question]";
echo "</h4><br>";
echo "<ul class=list-group>";
echo "<li class=list-group-item>1) $row[option1]</li>";
echo "<li class=list-group-item>2) $row[option2]</li>";
echo "<li class=list-group-item>3) $row[option3]</li>";
echo "<li class=list-group-item>4) $row[option4]</li>";
echo "<li class=list-group-item>5) $row[option5]</li>";
echo "<div id=myDIV style=display:none><li class=list-group-item><b>Correct Option : ($row[correct_option])</b></div></li>";
echo "<li class=list-group-item><button onclick=toggleAnswer()>View Answer</button></li>";
echo "</ul>";
echo "<hr color=#FF0000 size=5 width=100%>";
}
?>
Would appreciate any suggestions...