50 users online (0 members and 50 guests)  

Thread: Back button.


  Results 1 to 2 of 2

Related

  1. Button back and next    Forum: Javascript Forum
    Replies: 1
  2. Disable Refresh and Back button??    Forum: HTML Forum
    Replies: 27
  3. back button problems    Forum: General Discussion
    Replies: 3
  4. back button problems    Forum: HTML Forum
    Replies: 1
  1. #1
    phpbeginner's Avatar
    New User

    Status
    Offline
    Join Date
    Jul 2008
    Posts
    1

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

    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.
    Last edited by phpbeginner; 07-09-2008 at 01:24 AM. Reason: editing

  2. #2
    jthayne's Avatar

    Status
    Offline
    Join Date
    Aug 2008
    Location
    Texas
    Posts
    508

    Re: Back button.

    The problem is that nowhere in there are you decreasing the count. What you need to do is track the current and previous question.

    Before I go any further though, I think it would be wise to look at your logic again. You want to decrement the number of correct answers if they go back one. What if they got the previous question wrong? It shouldn't change the number of correct answers then.

    Double-check your logic and run through it again to make sure you are trying to get it to do what you need it to.

    What I recommend is instead of tracking the total number of correct in the SESSION variable, track whether each question was answered correctly:

    $_SESSION['question']['1'] = 1; // For correct answer
    $_SESSION['question']['2'] = 0; // For incorrect answer

    Then you can count the number of 1's to determine the number of correct answers. Additionally, you can set $_SESSION['question']['2'] = 0; every time you bring up the page. That will successfully decrement the number of correct questions as well as track which ones were correctly answered.



Tags for this Thread