Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 117,613 PHP Programmers for FREE! Ask your question and get quick answers from experts. There are 1,969 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



back button problem

 
Reply to this topicStart new topic

back button problem

confusedofphp
post 9 Jul, 2008 - 12:52 AM
Post #1


New D.I.C Head

*
Joined: 9 Jul, 2008
Posts: 2

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….

ques1.php
php
<html>
<head>
<title>Question 1 </title>
</head>
<body>
<form action="ques2.php" method="GET">
<font size="3" face="Verdana">
<?php
session_start();
echo "Current User : ".$_SESSION['name'];
?>
<br><br><b>Question 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
php
<html>
<head>
<title>Question2</title>
</head>
<body>
<form action="ques3.php" method="GET">
<font size="3" face="Verdana">
<br><br><b>Question 2</b><br><br>
</font>
<font size="2" face="Verdana">
What is PHP?<br><br>
<input type="radio" name="ques2" value="a">Personal 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><br>
<INPUT TYPE=SUBMIT VALUE="submit">
<input type="button" name="back" value="Back" onClick="history.go(-1);"><br>
</font>
</form>
<?php
require 'db.php';
session_start();
$counter=0;
$answer1=$_GET['ques1'];

$result=mysql_query("select ans from answer where qid='q1'");
$res=mysql_fetch_array($result);

if($answer1==$res["ans"])
{
$counter=$counter+1;
$_SESSION['ans']=$counter;
}
else
{
$_SESSION['ans']=$counter;
}
echo "Current User : ".$_SESSION['name']."<br>";
echo "Number of correct".$_SESSION['ans'];
?>
</body>
</html>



ques3.php
php
<html>
<head>
<title>Question3</title>
</head>
<body>
<form action="ques4.php" method="GET">
<font size="3" face="Verdana">
<br><br><b>Question 3</b><br><br>
</font>
<font size="2" face="Verdana">
What is MySQL?<br><br>
<input type="radio" name="ques3" value="a">Markup 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="history.go(-1);"><br>
</font>
</form>
<?php
require 'db.php';
session_start();
$answer2=$_GET['ques2'];
//$counter=$_SESSION['ans'];

$result=mysql_query("select ans from answer where qid='q2'");
$res=mysql_fetch_array($result);

if($answer2==$res["ans"])
{
//$counter=$counter+1;
$_SESSION['ans']++;
}
echo "Current User : ".$_SESSION['name']."<br>";
echo "Number of correct".$_SESSION['ans'];
?>
</body>
</html>




What should I change in my code?

Thank you.

Mod Edit: Please code.gif
Thanks, gabehabe smile.gif
User is offlineProfile CardPM

Go to the top of the page


BetaWar
post 9 Jul, 2008 - 10:55 AM
Post #2


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,569



Thanked 62 times

Dream Kudos: 975
My Contributions


This is because you are using get variables to passthe answer between pages, so when you go back the get variable is still set (it is placed in the url) and thus it adds to the correct answers.

Right now you have a url like so:

www.something.com/ques2.php?ques1=a
When you go forward it goes to the next page
www.something.com/ques3.php?ques2=d
But, when yoyu press th eback button the url is the same and you go to
www.something.com/ques2.php?ques1=a

If you are wanting to get things working better (probably not perfect until you tweak it a bit) you should use the post method:

CODE
<form action="ques4.php" method="POST">


and you retrieve the varaibles like so:
CODE
$answer2=$_POST['ques2'];


Hope that helps.
User is offlineProfile CardPM

Go to the top of the page

Dancia
post 9 Jul, 2008 - 02:07 PM
Post #3


D.I.C Head

**
Joined: 15 Jun, 2008
Posts: 57



Thanked 1 times
My Contributions


Ant if you need any or don't know if its $_GET or $_POST, use $_REQUEST
User is offlineProfile CardPM

Go to the top of the page

confusedofphp
post 14 Jul, 2008 - 09:53 PM
Post #4


New D.I.C Head

*
Joined: 9 Jul, 2008
Posts: 2

eventhough i use post method or request, still i get the same result..any other idea pls. tq.
User is offlineProfile CardPM

Go to the top of the page

mattisdada
post 18 Jul, 2008 - 06:30 AM
Post #5


New D.I.C Head

*
Joined: 30 May, 2008
Posts: 36


My Contributions


Couldnt you just use a DB system like MySQL. When they get the ansewer right it updates the DB, stating that the user doing it, go Question 1.B correct. And it would be able to hold that data forever as well if needed. And held securely. As it is an exam:P Dont want cheating going on.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/7/08 11:52PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month