i am dealing with online quiz project in this project the user have to select the option & then he submits the form according to selected option the marks are given. But my problem is that after submitting the form whenever the user hits refresh button the form is resubmitted bcoz of this the duplicate values are inserted in the database & also it displays wrong result. I am trying this from last 2 days but i cannt get solution. Plz help me if it is possible
Thnax
Coding is as follows:
<?
session_start();
class paper
{
function show_paper()
{
$var_optVal = $_POST['opt_id'];
?>
<html>
<head>
<script language="javascript">
function radio_check(var_val)
{
var select_option = var_val;
}
</script>
</head>
<body>
<br>
<form name="paperfrm" action="paper.php" method="POST">
<table width="70%" height="12%" border="0" cellspacing="1" cellpadding="0" align="center">
<tr>
<? // values from login_class.php
if (isset($_POST['action']) && $_POST['action'] == 'submitted')
{
$sub_id = $_POST['sub_id'];
$level_id = $_POST['level_id'];
$cat_id = $_POST['cat_id'];
$stud_id = $_POST['stud_id'];
}
else
{
$sub_id = $_GET['sub_id'];
$level_id = $_GET['level_id'];
$cat_id = $_GET['cat_id'];
$stud_id = $_GET['stud_id'];
}
//pagination starts here
$Per_Page=1;
$SQL="SELECT COUNT(*) AS Total FROM question where sub_id='".$sub_id."' and level_id='".$level_id."'";
$SQL_Result=mysql_query($SQL);
$SQL_Result_Array=mysql_fetch_array($SQL_Result);
$Total=$SQL_Result_Array['Total'];
$SQL="SELECT * FROM question where sub_id='".$sub_id."' and level_id='".$level_id."'";
if(empty($_POST['Result_Set']))
{
$Result_Set=0;
$SQL.=" LIMIT $Result_Set, $Per_Page";
}else
{
$Result_Set=$_POST['Result_Set'];
$SQL.=" LIMIT $Result_Set, $Per_Page";
}
//echo "<td>".$SQL."</td>";
// Run The Query With a Limit to get result
$SQL_Result=mysql_query($SQL);
$SQL_Rows=mysql_num_rows($SQL_Result);
//******
//pagination to be continue
while($row = mysql_fetch_array($SQL_Result))
{
$question = $row['question'];
$que_id = $row['que_id'];
?>
<td class="tdtext"><?=$question?></td>
</tr>
<? } //end of while?>
<tr>
<?
//to get options from option table
$query = "SELECT * FROM `option` WHERE `que_id` ='$que_id'";
$execute = mysql_query($query)or die("couldn't execute a query".mysql_error());
$num_rows = mysql_num_rows($execute);
while($row1 = mysql_fetch_array($execute))
{
$option = $row1['option'];
$opt_id = $row1['opt_id'];
?>
<td class="tdtext"><input type="radio" name="opt_id" value="<?=$opt_id?>" onClick="radio_check(this.value)" <? if($var_optVal == '$opt_id') echo 'checked';?> ><?=$option?>
</td>
</tr>
<? } //end of while ?>
<tr>
<?
//pagination continue
//make common fun as total,result_set,per_page i/p, return value as res1 ie Result_Set
if ($Total>0)
{
//echo $Result_Set;
if($Result_Set<$Total && $Result_Set>0)
{
$Res1=$Result_Set - $Per_Page;
//echo $Res1."res1";
?>
<A HREF="paper.php?Result_Set=<?echo $Res1;?>&sub_id=<?echo $sub_id?>&level_id=<?echo $level_id?>"><< Previous Page</A>
<?
}
# Links
$Pages=$Total / $Per_Page;
if ($Pages>1)
{
for($b=0,$c=1; $b < $Pages; $b++,$c++)
{
$Res1=$Per_Page * $b;
//echo $Res1."res1";
?>
<A HREF="paper.php?Result_Set=<?echo $Res1?>&sub_id=<?echo $sub_id?>&level_id=<?echo $level_id?>"><?//echo $c ?></A>
<?
}
}
if ($Result_Set>=0 && $Result_Set<$Total)
{
$Res1=$Result_Set+$Per_Page;
if ($Res1<$Total)
{
?>
<!--<A HREF="paper.php?Result_Set=<?echo $Res1;?>&sub_id=<?echo $sub_id;?>&level_id=<?echo $level_id;?>&option=<?echo $var_optVal;?>&que_id=<?echo $que_id;?>" > Next Page >></A>-->
<?
}
}
}
//pagination ends
?>
</tr>
<tr align="center">
<td>
<input type="hidden" name="Result_Set" value="<?=$Res1?>">
<input type="hidden" name="cat_id" value="<?=$cat_id?>">
<input type="hidden" name="sub_id" value="<?=$sub_id?>">
<input type="hidden" name="que_id" value="<?=$que_id?>">
<input type="hidden" name="level_id" value="<?=$level_id?>">
<input type="hidden" name="stud_id" value="<?=$stud_id?>">
<input type="hidden" name="action" value="submitted">
<input type="submit" name="submit" value="Submit & Continue" class="button">
<input type="reset" name="reset" value="Reset" class="button">
</td>
</tr>
<tr>
<td></td>
</tr>
</table>
</form>
</body>
</html>
<?
} //end of function show_paper
function ans()
{
//echo "test";exit;
$stud_opt_id = $_POST['opt_id'];
$que_id = $_POST['que_id'];
$stud_id = $_POST['stud_id'];
$cat_id = $_POST['cat_id'];
$sub_id = $_POST['sub_id'];
$date_time = date("Y-m-d h:i:s");
$query = "INSERT INTO `exam` (`stud_id` , `cat_id` , `sub_id` , `date_time` ) VALUES ('$stud_id', '$cat_id', '$sub_id', '$date_time')";
$result = mysql_query($query) or die("couldn't execute a query".mysql_error());
//echo $query;exit;
$exam_id = mysql_insert_id();
$sql_ans = "INSERT INTO `stud_ans` (`exam_id` , `que_id` , `option_id` ) VALUES ('$exam_id', '$que_id', '$stud_opt_id')";
$result_ans = mysql_query($sql_ans) or die("couldn't execute a query".mysql_error());
$stud_ans = "SELECT * FROM `option` WHERE `opt_id` = '$stud_opt_id'";
$execute = mysql_query($stud_ans) or die("couldn't execute a query".mysql_error());
// echo $stud_ans;exit;
$row = mysql_fetch_object($execute);
$correct_option = $row->correct_option;
//to check whether the selected option is correct & to calculate marks
// echo "testing";exit;
if($correct_option=='T')
{
$_SESSION['marks'] = $_SESSION['marks']+10;
}
elseif($correct_option=='F')
{
$_SESSION['marks'] = $_SESSION['marks']+0;
}
$marks = $_SESSION['marks'];
return $marks;
?>
<!--<table><tr><td><?//=$marks?></td></tr></table>-->
<?
} //end of function ans
} //end of class paper
//echo $POST['attempt'];exit;
/ if($POST['attempt'] > 1){
print 'you already submitted!';
exit;
}
/
if (isset($POST['action']) && $POST['action'] == 'submitted')
{
//echo $_POST['action'];exit;
$objans = new paper();
$total = $objans->ans();?>
<table><tr><td><? echo $total;?></td></tr></table>
<?
} //end of if
?>