reineskye25 Posted April 23, 2012 Share Posted April 23, 2012 Hi guys. I have a problem in my web based examanation system written in php. Here's the scenario: when a user takes an exam. 1 question is displayed 1 at a time. my problem is if he/she is at the 2nd question then he/she pressed the back button he/she is redirected back to the 1st question and my system just go crazy. How can i prevent the user from pressing the back button or returning to the previous questions? Please I really need help on this. Can anyone give me suggestion for possible solutions on my problem? Thanks in advance by the way here's my code, you can also download it from attachment. ------------------------------------------process_exam.php------------------------------------------------------------ <?php //start session session_start(); //declare variables //iCorrect= variable for score $_SESSION['iCorrect']=0; //a=variable for iteration kang index kang mga array or serves as an current item number $_SESSION['a']=1; //connect to database include('mysql_connect.php'); $result = mysql_query("select * from questions where exam_id ='$_SESSION[exam_id]'"); //count the number of found questions $_SESSION['num_of_questions']=mysql_num_rows($result); //declare the iterator $i=1; //get the options for the question1,question2 and so on and so on. depende kung ilang questions ang mafetch nya sa dbase.so pde kht ilang questions lagay ng user while ($row = mysql_fetch_assoc($result)) { $questions[$i] = $row['question']; $op1[$i] = $row['op1']; $op2[$i] = $row['op2']; $op3[$i] = $row['op3']; $op4[$i] = $row['op4']; $answers[$i]=$row['answer']; $i++; } //view the data's in the array for debugging purposes echo 'answers: '; print_r($questions); echo '<br>'; print_r($op1); echo '<br>'; print_r($op2); echo '<br>'; print_r($op3); echo '<br>'; print_r($op4); echo '<br>'; print_r($answers); echo '<br>'; echo '<br>'; //pass the arrays to session arrays to be used in other php pages $_SESSION['question']=$questions; $_SESSION['op1']=$op1; $_SESSION['op2']=$op2; $_SESSION['op3']=$op3; $_SESSION['op4']=$op4; $_SESSION['answers']=$answers; //print the arrays. debugging purposes //1 is the starting index of the array because the $i is equal to 1 print_r($_SESSION['question']); echo '<br>'; print_r($_SESSION['op1']); echo '<br>'; print_r($_SESSION['op2']); echo '<br>'; print_r($_SESSION['op3']); echo '<br>'; print_r($_SESSION['op4']); echo '<br>'; print_r($_SESSION['answers']); header("location:exam.php"); ?> </body> </html> ----------------------------------------------------------exam.php-------------------------------------------------------------------- <?php //start session session_start(); echo '<br>'; echo '<br>'; echo '<tr>'; echo '<b>'; echo 'Question: '; echo '<td align = center>'.$_SESSION['question'][$_SESSION['a']].'</td>'; echo '</tr>'; echo '<form method = "post" action= "nxt_question.php">'; echo '<table border = "0">'; echo '<tr>'; //echo '<td>'; echo "<td><Input type =\"Radio\" Name =\"choice\" value='".$_SESSION['op1'][$_SESSION['a']]."'>"; echo '<td>'.$_SESSION['op1'][$_SESSION['a']].'</td>'; echo '</tr>'; echo '<tr>'; echo "<td><Input type =\"Radio\" Name =\"choice\" value='".$_SESSION['op2'][$_SESSION['a']]."'>"; echo '<td>'.$_SESSION['op2'][$_SESSION['a']].'</td>'; echo '</td>'; echo '</tr>'; echo "<td><Input type =\"Radio\" Name =\"choice\" value='".$_SESSION['op3'][$_SESSION['a']]."'>"; echo '<td>'.$_SESSION['op3'][$_SESSION['a']].'</td>'; echo '</tr>'; echo "<td><Input type =\"Radio\" Name =\"choice\" value='".$_SESSION['op4'][$_SESSION['a']]."'>"; echo '<td>'.$_SESSION['op4'][$_SESSION['a']].'</td>'; echo '</tr>'; echo '</table>'; echo '<tr> <td> <input type = "submit" value="Next Question"> </td> </tr>'; echo '</form>'; ?> ----------------------------------------------nxt_question.php------------------------------------- <?php //start session session_start(); //Checking of answers kada mag pindot next question button if(isset($_POST['choice'])) { $ans_of_user=$_POST['choice']; $_SESSION['answers'][$_SESSION['a']]; if (strcmp($ans_of_user,$_SESSION['answers'][$_SESSION['a']])==0) { $_SESSION["iCorrect"]++; } } //add 1 to the session a. session a is the iterator. serves as indexes of the arrays that stores the questions,answers etc... $_SESSION['a']++; //ridirect to exam.php if there are still number of items remaining. while($_SESSION['a']<=$_SESSION['num_of_questions']) { header("location:exam.php"); exit(); //kill the script } echo 'exam finished!'; echo '<br>'; echo 'Your score is: '; echo $_SESSION["iCorrect"]; //unset the session variables needed to be reset unset($_SESSION["iCorrect"]); unset($_SESSION["exam_id"]); unset($_SESSION["invitation_code"]); unset($_SESSION["a"]); unset($_SESSION["num_of_questions"]); unset($_SESSION["question"]); unset($_SESSION["op1"]); unset($_SESSION["op2"]); unset($_SESSION["op3"]); unset($_SESSION["op4"]); unset($_SESSION["answers"]); ?> 18166_.php 18167_.php 18168_.php Quote Link to comment https://forums.phpfreaks.com/topic/261459-help-in-my-web-based-examination-system-written-in-php/ Share on other sites More sharing options...
MMDE Posted April 23, 2012 Share Posted April 23, 2012 Do you want them to be able to go back to the previous question without it failing, or do you not want them to be able to go back to the previous question? Is the problem only that if they use the back button the system fails? What I would have done is to have it all in one php file, and then record in a database what question they are at. If they try to deliver an answer for a question they are not at, it will fail (hidden input with the id/number of the question), and by fail I mean nothing will happen except for it loading the question they currently are at. They can eve leave and close the page and come back to it and still be at the same question, because it's saved on the server side and only one php file handles it. Quote Link to comment https://forums.phpfreaks.com/topic/261459-help-in-my-web-based-examination-system-written-in-php/#findComment-1339779 Share on other sites More sharing options...
reineskye25 Posted May 3, 2012 Author Share Posted May 3, 2012 hello sir sorry for my very late reply. Thanks also for your reply in my question. Nwei regarding my problem, I want them to be able to go back to the previous question without my system failing Thanks again sir for your suggestion. I will try it ill post here the results Quote Link to comment https://forums.phpfreaks.com/topic/261459-help-in-my-web-based-examination-system-written-in-php/#findComment-1342630 Share on other sites More sharing options...
reineskye25 Posted May 3, 2012 Author Share Posted May 3, 2012 Do you want them to be able to go back to the previous question without it failing, or do you not want them to be able to go back to the previous question? Is the problem only that if they use the back button the system fails? What I would have done is to have it all in one php file, and then record in a database what question they are at. If they try to deliver an answer for a question they are not at, it will fail (hidden input with the id/number of the question), and by fail I mean nothing will happen except for it loading the question they currently are at. They can eve leave and close the page and come back to it and still be at the same question, because it's saved on the server side and only one php file handles it. hello sir sorry for my very late reply. Thanks also for your reply in my question. Nwei regarding my problem, I want them to be able to go back to the previous question without my system failing Thanks again sir for your suggestion. I will try it and ill post here the results. But I think there will still be a problem sir. if the user clicks the back button, the array that is holding the "user answers" will be affected. hmm. Quote Link to comment https://forums.phpfreaks.com/topic/261459-help-in-my-web-based-examination-system-written-in-php/#findComment-1342635 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.