Hi. I am working on my Online Examination project in my web development subject but I am having a hard time to code how to go back to my previous page where I put session on it.

my online exam are categorize by subject then inside the subject there is a test name. FOr example:

Subject: Math
    -Algebra
    -Calculus

In admin side, if I want to view and edit all the question, the link will direct me to "view_subject.php" here's the code:

<?php
<h2>Subjects</h2>
  <table width="1000" height="104" bordercolor="#666666" border="0">

   <tr bordercolor="#666666">
  <center>
    <td width="100" height="33" bgcolor=" #d4d6f8" align="center"><b>Subject ID</b></td>
    <td width="120" bgcolor=" #e1e2f7" align="center"><b>Subject Name</b></td>
    <td width="88" bgcolor=" #e1e2f7" align="center"><b>VIEW TESTS</b></td>
  </center>
  </tr>
   <?php
  $result=mysql_query("SELECT count(subject_id) as subject_id FROM subject");
while($row=mysql_fetch_array($result))
{$subject_id=$row['subject_id'];}


 $result=mysql_query("SELECT * from subject");
while($row=mysql_fetch_array($result))
{
 ?>

  <tr>
    <td height="35" align="center"><?php echo $row['subject_id']; ?></td>
    <td ><?php echo $row['sub_name']; ?></td>


      <td align="center"><a href="view_test.php?id=<?php echo $row['subject_id']?>" >VIEW</a></td>
  </tr>

<?php
}

?>

When the admin will view the test under the particular subject, the admin just have to click the view.

<a href="view_test.php?id=<?php echo $row['subject_id']?>" >VIEW</a>

and then he will redirect to "view_test.php" where she will see the tests under the subject. here's the code:

<?php
<h2>List of Tests</h2>
  <table width="1000" height="104" bordercolor="#666666" border="0">

   <tr bordercolor="#666666">
  <center>
    <td width="100" height="33" bgcolor=" #d4d6f8" align="center"><b>Test ID</b></td>
    <td width="120" bgcolor=" #e1e2f7" align="center"><b>Test Name</b></td>
    <td width="120" bgcolor=" #e1e2f7" align="center"><b>View Question</b></td>
  </center>
  </tr>
  <?php
  $id=$_GET['id'];
  $_SESSION['view_id']=$id;
  $result=mysql_query("SELECT * FROM test WHERE subject_id='$id'");
  while($row=mysql_fetch_array($result))
{


  ?>

   <?php
  $result=mysql_query("SELECT count(test_id) as test_id FROM test");
while($row=mysql_fetch_array($result))
{$test_id=$row['test_id'];}


 $result=mysql_query("SELECT * FROM test WHERE subject_id LIKE '$id'");
while($row=mysql_fetch_array($result))
{
 ?>

  <tr>
    <td height="35" align="center"><?php echo $row['test_id']; ?></td>
    <td ><?php echo $row['test_name']; ?></td> 
     <td align="center"><a href="view_test_ques.php?id=<?php echo $row['test_id']?>" >VIEW</a></td>
  </tr>

<?php
}
} 
?>

almost the same to view_subject.php. then when she click again the view , she will see the questions on the particular test. The code will redirect her to view_test_ques.php. here's the code:

<?php
<h2>Test Questions</h2>
  <table width="1000" height="104" bordercolor="#666666" border="0">

   <tr bordercolor="#666666">
  <center>
    <td width="80" height="33" bgcolor=" #d4d6f8" align="center"><b>Question id</b></td>
    <td width="80" bgcolor=" #e1e2f7" align="center"><b>test id</b></td>
    <td width="200" bgcolor="#d4d6f8" align="center"><b>question</b></td>
    <td width="120" bgcolor=" #e1e2f7" align="center"><b>choice 1</b></td>
    <td width="120" bgcolor=" #d4d6f8" align="center"><b>choice 2</b></td>
    <td width="120" bgcolor=" #e1e2f7" align="center"><b>chocie 3</b></td>
    <td width="120" bgcolor=" #e1e2f7" align="center"><b>choice 4</b></td>
    <td width="60" bgcolor=" #d4d6f8" align="center"><b>answer</b></td>
    <td width="60" bgcolor=" #e1e2f7" align="center"><b>edit</b></td>
    <td width="80" bgcolor=" #e1e2f7" align="center"><b>status</b></td>
  </center>
  </tr>
    <?php
  $id=$_GET['id'];
  $_SESSION['view_id']=$id;
  $result=mysql_query("SELECT * FROM tblquestion WHERE test_id='$id'");
  while($row=mysql_fetch_array($result))
{


  ?>
   <?php
  $result=mysql_query("SELECT count(ques_id) as ques_id FROM tblquestion");
while($row=mysql_fetch_array($result))
{$ques_id=$row['ques_id'];}


 $result=mysql_query("SELECT * from tblquestion where test_id LIKE '$id'");
while($row=mysql_fetch_array($result))
{
 ?>

  <tr>
     <td height="35" align="center"><?php echo $row['ques_id']; ?></td>

    <td align="center" ><?php echo $row['test_id']; ?></td>
    <td ><?php echo $row['ques']; ?></td>
    <td ><?php echo $row['choice1']; ?></td>
    <td ><?php echo $row['choice2']; ?></td>
    <td ><?php echo $row['choice3']; ?></td>
    <td ><?php echo $row['choice4']; ?></td>
    <td align="center"><?php echo $row['answer']; ?></td>
     <td align="center"><a href="edit_ques.php?id=<?php echo $row['ques_id']?>" >EDIT</a></td>
     <td align="center" ><a href="status.php?status=<?php echo $row['ques_id']?>"> <?php echo $row['status']; ?></td>
  </tr>

<?php
}
 }
?>

Now, I see all the question on a particular test. Now There's an edit link. I want to edit the particular question. I just have to click the "EDIT" which will redirect me to edit_ques.php. Once I already edit the questions. The program will prompt me a message box coz I put a javascript code.

<script type="text/javascript">
alert("Test Question has been Updated!");
window.location="view_test_ques.php";
</script>

Now, here's the problem. The message box will prompt me that the "Test question has been updated!". and it will redirect me to view_test_ques.php but when I click ok to the message box, the browser will show me that "Object Not Found".

I know that there's something wrong in my javascript code. Can you help me.? I know its about session. But I really don't know what to do.. Thank you..

Member Avatar for LastMitch

I know that there's something wrong in my javascript code. Can you help me.? I know its about session. But I really don't know what to do.. Thank you..

This is an homework assignment.

Was php session part of your homework assignment or using javascript to redirected is?

I'm bit confused why do you need session to do that you already have it in place.

This is just an example base on your code

//Your form
<form method="POST" action="redirected.php">
<input type="submit" name="post" value="Submit">
</form>

// redirected.php file
<?php
session_start();
$id=$_GET['id'];
$_SESSION['view_id']=$_POST;

//redirected
$redirect = $_SESSION['view_id'];
$redirect .= "?php=".session_id(); // The url file with session_id: view_test_ques.php
header("Location: $redirect");
exit();
?>

You just want to redirected to the previous page after when you click submit.

Since you are using javascript to redirected you don't necessary need php session in your code to do that.

What file has that javascript code? I don't see it anywhere.

It should be a <input> tags or <form> tags.

Can you post that file that has the javascript.

It's my project. Iam the one who actually code this. sorry if I made you confused. But thank you for helping..

I was thinking if it is possible to put a php variable inside a js code..?

Member Avatar for LastMitch

so it's not a session but javascript?

OK then try this:

<script type="text/javascript">
function Redirect() {
    window.location="folder/view_test_ques.php";
}
</script>

<form>
<input type="button" value="Submit" onclick="Redirect();" />
</form>

or you can echo it:

<?php echo '<script type="text/javascript">
function Redirect() {
    window.location="folder/view_test_ques.php";
}
</script>'; 
?>

Thank you..

i have another question,

i am trying this code:

<script type="text/javascript">
alert("Test Question has been Updated!");
window.location="view_test_ques.php?id=<?php echo $test_id ?>";
</script>

it will redirect me to view_test_ques.php where the test_id is equal to the assigned test_id of the particular question. but it doesn't show right results. the code, gets the id of the question not the test_id. What should i do?

Hey! I already debug the error. :) I don't use the code you give but I use it as preference to understand that you can put a php variable in a js code. I changed the $id into $tid in view_test.php & view_test_ques.php and also in my js code:

<script type="text/javascript">
alert("Test Question has been Updated!");
window.location="view_test_ques.php?tid=<?php echo $tid ?>";
</script>

Thanks for helping.. I finally got an idea.. :) Thanks a lot. :D

I thought of using a div or alert moz-box code instead of alert box coz they are really fading off...but If you still mind

<div class="flash-messages afp-p-now">
        <div class="flash">Test Question has been Updated! <a href="view_test_ques.php?tid=<?php echo $tid ?>">view</a> 
        <span class="nof-close close"></span>
        </div>
        </div>

        Ajax function

        <script>
         $('.nof-close').click(function(){
            $('.afp-p-now').fadeOut();
            $('.afp-p-now').css('display','none');

        }); 
        </script>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.