Click here to Skip to main content
15,877,859 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a simple quiz in PHP.The user is able to log in.It displays one question per page.Then it goes for review.It allows the user to review one question per page.Each page has a next question button and previous question button(except first page).

Now I wish to retain already selected answers when I tap on previous question.

//code for Next question

if($submit=='Next Question' && isset($ans))
{
mysql_data_seek($rs,$_SESSION[qn]);
$row= mysql_fetch_row($rs);
mysql_query("insert into mst_useranswer(sess_id, test_id, que_des, ans1,ans2,ans3,ans4,true_ans,your_ans) values ('".session_id()."', $tid,'$row[2]','$row[3]','$row[4]','$row[5]', '$row[6]','$row[7]','$ans')") or die(mysql_error());
if($ans==$row[7])
{
$_SESSION[trueans]=$_SESSION[trueans]+1;
}

$_SESSION[yans][$p]=$ans;//$p is $_SESSION[qn]+1

$_SESSION[qn]=$_SESSION[qn]+1;
}

// code for previous question


if($_POST['prev']=='Previous Question')
{

$_SESSION[qn]=$_SESSION[qn]-1;
}

//code when i Load the quiz
if(isset($_SESSION[yans][$p]))
{
$a = $_SESSION[yans][$p]; // $p = $_SESSION[qn]+1
//echo $p;
}
echo "Que ". $n .": $row[2]</style>";
if($a==1)
{
echo "<input type=radio name=ans value=1 checked='checked'>$row[3] ";
}
else
{
echo "<input type=radio name=ans value=1>$row[3]";
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900