473,378 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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

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

Similar topics

25
by: KK | last post by:
Hi, I am using history.go(-1) for implementing the back button functionality. Its working fine but with this exception. 1. The page which is having back button has some hyperlinks on it. ...
1
by: Peter D. Dunlap | last post by:
Hello, I realize that this may not be the best place to ask this question, through the application is asp.net. I also realize that questions about disabling the back button are generally met...
4
by: Hypo | last post by:
I added a 'Back' button to my page, and wrote 'OnClick' code something like this: Response.Write("<script>history.go(-" + iDepthIndex.ToString() + ");</script>"); But, it dosnt work! Effect...
8
by: Galina | last post by:
Hello I have 6 dependent list boxes on my ASP page:  Faculty;  Lecturer;  Course;  Course occurrence;  Group;  Week commencing date. When faculty is selected, lists of lecturers and...
6
by: Mark | last post by:
I have two dropdown lists. Both have autopostback set to true. In both dropdowns, when you select an item from the list, it redirects to the Value property of the dropdown. Nothing fancy. ...
29
by: Tom wilson | last post by:
I can't believe this is such an impossibility... I have an asp.net page. It accepts data through on form fields and includes a submit button. The page loads up and you fill out some stuff. ...
6
by: tshad | last post by:
I am trying to set up a Javascript popup box that has a way of sending back a message to asp.net on how to process some data. At the moment I am just doing: ...
5
by: Tom wilson | last post by:
I'm developing an application that is a benefits enrolment website. The benefits can be of any type in any order as stored in a SQL server. On each page is a Back and Next button. At a certain...
7
by: Sridhar | last post by:
Hi, I have a question regarding the Page_Load method and Back Button of Internet explorer. I have created a webform. In that webform I have several Text Boxes. Also I have two buttons. When I...
5
by: ns21 | last post by:
How can the browser back button be disabled If the form is submitting information to other pages or submitting to itself or using redirections. I tried the tweaks like history.forward(1) in each...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.