back button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • confusedofphp
    New Member
    • Jul 2008
    • 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….
    [code=php]
    ques1.php
    <html>
    <head>
    <title>Questi on 1 </title>
    </head>
    <body>
    <form action="ques2.p hp" method="GET">
    <font size="3" face="Verdana">
    <?php
    session_start() ;
    echo "Current User : ".$_SESSION['name'];
    ?>
    <br><br><b>Ques tion 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>Question 2</title>
    </head>
    <body>
    <form action="ques3.p hp" method="GET">
    <font size="3" face="Verdana">
    <br><br><b>Ques tion 2</b><br><br>
    </font>
    <font size="2" face="Verdana">
    What is PHP?<br><br>
    <input type="radio" name="ques2" value="a">Perso nal 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><b r>
    <INPUT TYPE=SUBMIT VALUE="submit">
    <input type="button" name="back" value="Back" onClick="histor y.go(-1);"><br>
    </font>
    </form>
    <?php
    require 'db.php';
    session_start() ;
    $counter=0;
    $answer1=$_GET['ques1'];

    $result=mysql_q uery("select ans from answer where qid='q1'");
    $res=mysql_fetc h_array($result );

    if($answer1==$r es["ans"])
    {
    $counter=$count er+1;
    $_SESSION['ans']=$counter;
    }
    else
    {
    $_SESSION['ans']=$counter;
    }
    echo "Current User : ".$_SESSION['name']."<br>";
    echo "Number of correct".$_SESS ION['ans'];
    ?>
    </body>
    </html>


    ques3.php
    <html>
    <head>
    <title>Question 3</title>
    </head>
    <body>
    <form action="ques4.p hp" method="GET">
    <font size="3" face="Verdana">
    <br><br><b>Ques tion 3</b><br><br>
    </font>
    <font size="2" face="Verdana">
    What is MySQL?<br><br>
    <input type="radio" name="ques3" value="a">Marku p 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="histor y.go(-1);"><br>
    </font>
    </form>
    <?php
    require 'db.php';
    session_start() ;
    $answer2=$_GET['ques2'];
    //$counter=$_SESS ION['ans'];

    $result=mysql_q uery("select ans from answer where qid='q2'");
    $res=mysql_fetc h_array($result );

    if($answer2==$r es["ans"])
    {
    //$counter=$count er+1;
    $_SESSION['ans']++;
    }
    echo "Current User : ".$_SESSION['name']."<br>";
    echo "Number of correct".$_SESS ION['ans'];
    ?>
    </body>
    </html>
    [/code]


    What should I change in my code?

    Thank you.
    Last edited by pbmods; Jul 9 '08, 07:19 AM. Reason: Added CODE tags.
  • developing
    New Member
    • Mar 2007
    • 110

    #2
    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

    Comment

    Working...