Hi all,
Im very new to php. Currently im creating an online exam system which uses php and mysql as the database. I have registration and login page which works pretty fine. I had problem when it comes to question page. I have about 20 questions. As soon the user login to the system, he should start answering. The system should remember the number of correct answers til the 20th question. I used session to hold the value of number of correct answers. For example :
Login.php login and redirect to ques1.php
ques1.php first question, click submit to redirect to ques2.php
ques2.php second question. Click submit to go to ques3.php or click back to return to ques1.php
The problem is when I click back button the counter which holds the value of correct answer doesn’t work properly.
Let say I’ve answered 3 questions correctly, the counter should show 3, then if click back button, it suppose become 2 but it doesn’t happen here. The counter increase becomes 4.
Code sample as below….
[code=php]
ques1.php
<html>
<head>
<title>Questi on 1 </title>
</head>
<body>
<form action="ques2.p hp" method="GET">
<font size="3" face="Verdana">
<?php
session_start() ;
echo "Current User : ".$_SESSION['name'];
?>
<br><br><b>Ques tion 1</b><br><br>
</font>
<font size="2" face="Verdana">
What is HTML?<br><br>
<input type="radio" name="ques1" value="a"> Hyper Text Markup Language<br>
<input type="radio" name="ques1" value="b"> Hyper Transfer Markup Language<br>
<input type="radio" name="ques1" value="c"> Hyper Text Transfer Language<br><br >
<INPUT TYPE=SUBMIT VALUE="submit">
</form>
</body>
</html>
ques2.php
<html>
<head>
<title>Question 2</title>
</head>
<body>
<form action="ques3.p hp" method="GET">
<font size="3" face="Verdana">
<br><br><b>Ques tion 2</b><br><br>
</font>
<font size="2" face="Verdana">
What is PHP?<br><br>
<input type="radio" name="ques2" value="a">Perso nal Home Page<br>
<input type="radio" name="ques2" value="b"> PHP Hypertext Preprocessor<br >
<input type="radio" name="ques2" value="c"> Personal Hypertext Processor<br><b r>
<INPUT TYPE=SUBMIT VALUE="submit">
<input type="button" name="back" value="Back" onClick="histor y.go(-1);"><br>
</font>
</form>
<?php
require 'db.php';
session_start() ;
$counter=0;
$answer1=$_GET['ques1'];
$result=mysql_q uery("select ans from answer where qid='q1'");
$res=mysql_fetc h_array($result );
if($answer1==$r es["ans"])
{
$counter=$count er+1;
$_SESSION['ans']=$counter;
}
else
{
$_SESSION['ans']=$counter;
}
echo "Current User : ".$_SESSION['name']."<br>";
echo "Number of correct".$_SESS ION['ans'];
?>
</body>
</html>
ques3.php
<html>
<head>
<title>Question 3</title>
</head>
<body>
<form action="ques4.p hp" method="GET">
<font size="3" face="Verdana">
<br><br><b>Ques tion 3</b><br><br>
</font>
<font size="2" face="Verdana">
What is MySQL?<br><br>
<input type="radio" name="ques3" value="a">Marku p Languange<br>
<input type="radio" name="ques3" value="b"> Programming Language<br>
<input type="radio" name="ques3" value="c"> Database<br><br >
<INPUT TYPE=SUBMIT VALUE="submit">
<input type="button" name="back" value="Back" onClick="histor y.go(-1);"><br>
</font>
</form>
<?php
require 'db.php';
session_start() ;
$answer2=$_GET['ques2'];
//$counter=$_SESS ION['ans'];
$result=mysql_q uery("select ans from answer where qid='q2'");
$res=mysql_fetc h_array($result );
if($answer2==$r es["ans"])
{
//$counter=$count er+1;
$_SESSION['ans']++;
}
echo "Current User : ".$_SESSION['name']."<br>";
echo "Number of correct".$_SESS ION['ans'];
?>
</body>
</html>
[/code]
What should I change in my code?
Thank you.
Im very new to php. Currently im creating an online exam system which uses php and mysql as the database. I have registration and login page which works pretty fine. I had problem when it comes to question page. I have about 20 questions. As soon the user login to the system, he should start answering. The system should remember the number of correct answers til the 20th question. I used session to hold the value of number of correct answers. For example :
Login.php login and redirect to ques1.php
ques1.php first question, click submit to redirect to ques2.php
ques2.php second question. Click submit to go to ques3.php or click back to return to ques1.php
The problem is when I click back button the counter which holds the value of correct answer doesn’t work properly.
Let say I’ve answered 3 questions correctly, the counter should show 3, then if click back button, it suppose become 2 but it doesn’t happen here. The counter increase becomes 4.
Code sample as below….
[code=php]
ques1.php
<html>
<head>
<title>Questi on 1 </title>
</head>
<body>
<form action="ques2.p hp" method="GET">
<font size="3" face="Verdana">
<?php
session_start() ;
echo "Current User : ".$_SESSION['name'];
?>
<br><br><b>Ques tion 1</b><br><br>
</font>
<font size="2" face="Verdana">
What is HTML?<br><br>
<input type="radio" name="ques1" value="a"> Hyper Text Markup Language<br>
<input type="radio" name="ques1" value="b"> Hyper Transfer Markup Language<br>
<input type="radio" name="ques1" value="c"> Hyper Text Transfer Language<br><br >
<INPUT TYPE=SUBMIT VALUE="submit">
</form>
</body>
</html>
ques2.php
<html>
<head>
<title>Question 2</title>
</head>
<body>
<form action="ques3.p hp" method="GET">
<font size="3" face="Verdana">
<br><br><b>Ques tion 2</b><br><br>
</font>
<font size="2" face="Verdana">
What is PHP?<br><br>
<input type="radio" name="ques2" value="a">Perso nal Home Page<br>
<input type="radio" name="ques2" value="b"> PHP Hypertext Preprocessor<br >
<input type="radio" name="ques2" value="c"> Personal Hypertext Processor<br><b r>
<INPUT TYPE=SUBMIT VALUE="submit">
<input type="button" name="back" value="Back" onClick="histor y.go(-1);"><br>
</font>
</form>
<?php
require 'db.php';
session_start() ;
$counter=0;
$answer1=$_GET['ques1'];
$result=mysql_q uery("select ans from answer where qid='q1'");
$res=mysql_fetc h_array($result );
if($answer1==$r es["ans"])
{
$counter=$count er+1;
$_SESSION['ans']=$counter;
}
else
{
$_SESSION['ans']=$counter;
}
echo "Current User : ".$_SESSION['name']."<br>";
echo "Number of correct".$_SESS ION['ans'];
?>
</body>
</html>
ques3.php
<html>
<head>
<title>Question 3</title>
</head>
<body>
<form action="ques4.p hp" method="GET">
<font size="3" face="Verdana">
<br><br><b>Ques tion 3</b><br><br>
</font>
<font size="2" face="Verdana">
What is MySQL?<br><br>
<input type="radio" name="ques3" value="a">Marku p Languange<br>
<input type="radio" name="ques3" value="b"> Programming Language<br>
<input type="radio" name="ques3" value="c"> Database<br><br >
<INPUT TYPE=SUBMIT VALUE="submit">
<input type="button" name="back" value="Back" onClick="histor y.go(-1);"><br>
</font>
</form>
<?php
require 'db.php';
session_start() ;
$answer2=$_GET['ques2'];
//$counter=$_SESS ION['ans'];
$result=mysql_q uery("select ans from answer where qid='q2'");
$res=mysql_fetc h_array($result );
if($answer2==$r es["ans"])
{
//$counter=$count er+1;
$_SESSION['ans']++;
}
echo "Current User : ".$_SESSION['name']."<br>";
echo "Number of correct".$_SESS ION['ans'];
?>
</body>
</html>
[/code]
What should I change in my code?
Thank you.
Comment