Jump to content

Back button in php


confusedofphp

Recommended Posts

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

<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

<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

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

 

Link to comment
https://forums.phpfreaks.com/topic/113860-back-button-in-php/
Share on other sites

stop the browser from caching these pages.

 

Put this at the top of all your pages.

header("Cache-Control: no-store, no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

 

Here's an example:

 

Quiz

 

To view the source of any of these files use

 

http://www.phpgfx.com/test/view.php?file=filename here

 

Link to comment
https://forums.phpfreaks.com/topic/113860-back-button-in-php/#findComment-585099
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.