473,703 Members | 3,355 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

back button

1 New Member
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 1807
developing
110 New Member
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
7581
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. 2. When anybody click on those links, it will open a new windown.
1
4283
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 with derision, so let me explain: (1) The site is actually a web-based application, not a "web site" per say, and is not accessable by the public. It is an application used internally by the company I work for, though it accessed by employees
4
7600
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 is that browser navigates to desired page as expected and go back from where it started! But, the 'Back' button on browser works normaly. I dont get it!!??
8
2575
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 courses are populated. When course is selected, lists of occurrences, groups and
6
10684
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. Let's say you select 1 of the items, and are properly redirected. You press the back button. I have three servers providing two different functionalities: 1. After pressing the back button, the item you selected in the dropdown is still selected.
29
3996
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. The submit button posts the page back to the server. The button code detects an entry error and sends the page back to the user. This all works. However, if the user presses the Back button at this point, we go back and all the form values are...
6
1942
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: myNextButton.Attributes.Add("onclick", "return confirm('Are you sure you want finish this test?');") to attach a popup box to a button. The problem is that the buttons only say OK and Cancel. OK will take me to the event tied to the button and Cancel,
5
2393
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 point in the initialization of the app, I determine a navigational structure; what benefit is first, second, etc., therefore, which page to display first, second and so on. In session memory I store an array of page URL's and a single value...
7
2486
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 click on button1, it populates the text boxes with person1 details. When I click on button2, it populates the text box with person2 details and redirects to webform2. In webform2 if I click on Back Button of Internet Explorer it should display...
5
3002
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 page to prevent user to navigate back but its not very effective. The server side scripting is done using ASP. you may ask that why at first place I want to disable the browser back
0
8749
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9243
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9109
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9002
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8956
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7853
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4420
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2434
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2057
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.