469,609 Members | 1,161 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

back button

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….
Expand|Select|Wrap|Line Numbers
  1. ques1.php
  2. <html>
  3. <head>
  4. <title>Question 1 </title>
  5. </head>
  6. <body>
  7. <form action="ques2.php" method="GET">
  8. <font size="3" face="Verdana">
  9. <?php
  10. session_start();
  11. echo "Current User : ".$_SESSION['name'];
  12. ?>
  13. <br><br><b>Question 1</b><br><br>
  14. </font>
  15. <font size="2" face="Verdana">
  16. What is HTML?<br><br>
  17. <input type="radio" name="ques1" value="a"> Hyper Text Markup Language<br>
  18. <input type="radio" name="ques1" value="b"> Hyper Transfer Markup Language<br>
  19. <input type="radio" name="ques1" value="c"> Hyper Text Transfer Language<br><br>
  20. <INPUT TYPE=SUBMIT VALUE="submit">
  21. </form>
  22. </body>
  23. </html>
  24.  
  25.  
  26. ques2.php
  27. <html>
  28. <head>
  29. <title>Question2</title>
  30. </head>
  31. <body>
  32. <form action="ques3.php" method="GET">
  33. <font size="3" face="Verdana">
  34. <br><br><b>Question 2</b><br><br>
  35. </font>
  36. <font size="2" face="Verdana">
  37. What is PHP?<br><br>
  38. <input type="radio" name="ques2" value="a">Personal Home Page<br>
  39. <input type="radio" name="ques2" value="b"> PHP Hypertext Preprocessor<br>
  40. <input type="radio" name="ques2" value="c"> Personal Hypertext Processor<br><br>
  41. <INPUT TYPE=SUBMIT VALUE="submit">
  42. <input type="button" name="back" value="Back" onClick="history.go(-1);"><br>
  43. </font>
  44. </form>
  45. <?php
  46. require 'db.php';
  47. session_start();
  48. $counter=0;
  49. $answer1=$_GET['ques1'];
  50.  
  51. $result=mysql_query("select ans from answer where qid='q1'");
  52. $res=mysql_fetch_array($result);
  53.  
  54. if($answer1==$res["ans"])
  55. {
  56.     $counter=$counter+1;
  57.     $_SESSION['ans']=$counter;
  58. }
  59. else
  60. {
  61. $_SESSION['ans']=$counter;
  62. }
  63. echo "Current User : ".$_SESSION['name']."<br>";
  64. echo "Number of correct".$_SESSION['ans'];
  65. ?>
  66. </body>
  67. </html>
  68.  
  69.  
  70. ques3.php
  71. <html>
  72. <head>
  73. <title>Question3</title>
  74. </head>
  75. <body>
  76. <form action="ques4.php" method="GET">
  77. <font size="3" face="Verdana">
  78. <br><br><b>Question 3</b><br><br>
  79. </font>
  80. <font size="2" face="Verdana">
  81. What is MySQL?<br><br>
  82. <input type="radio" name="ques3" value="a">Markup Languange<br>
  83. <input type="radio" name="ques3" value="b"> Programming Language<br>
  84. <input type="radio" name="ques3" value="c"> Database<br><br>
  85. <INPUT TYPE=SUBMIT VALUE="submit">
  86. <input type="button" name="back" value="Back" onClick="history.go(-1);"><br>
  87. </font>
  88. </form>
  89. <?php
  90. require 'db.php';
  91. session_start();
  92. $answer2=$_GET['ques2'];
  93. //$counter=$_SESSION['ans'];
  94.  
  95. $result=mysql_query("select ans from answer where qid='q2'");
  96. $res=mysql_fetch_array($result);
  97.  
  98. if($answer2==$res["ans"])
  99. {
  100.     //$counter=$counter+1;
  101.     $_SESSION['ans']++;
  102. }
  103. echo "Current User : ".$_SESSION['name']."<br>";
  104. echo "Number of correct".$_SESSION['ans'];
  105. ?>
  106. </body>
  107. </html>
  108.  

What should I change in my code?

Thank you.
Jul 9 '08 #1
1 1499
developing
110 100+
if you are storing the questions in a database, then you dont need a separate file for each question.

make one file called ques.php and use the same code. add a couple more GET/POST variables that identify

current question
user answer
current question number
next question

and maybe a few other things

this way it will be less work for you and way less maintenance for you in the future.

a better way to track counter would be to have an array (session variable) that associates the 'questions already asked' with the 'sequence number' they were asked in.

if the 'current question number' already exists in the said array, then display the 'sequence number' for it
else add the 'current question number to the array
Jul 9 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

25 posts views Thread by KK | last post: by
1 post views Thread by Peter D. Dunlap | last post: by
4 posts views Thread by Hypo | last post: by
29 posts views Thread by Tom wilson | last post: by
6 posts views Thread by tshad | last post: by
5 posts views Thread by Tom wilson | last post: by
7 posts views Thread by Sridhar | last post: by
5 posts views Thread by ns21 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.