0

I am working on an e-testing system in which when a candidate login the test for which he/she was register will be shown to him/her.

I am getting the specific test through session to show the questions containing in that test. This was successfully done with the help of some "if" checks and two while loops(1 for questions and 2nd for answers against that question) having the form of radio buttons for selecting an answer.

Now each time when loop execute it create a form for single question and its answer, that way for multiple questions it results in multiple.

I am getting the checked radio buttons through ajax and posting it to another php file but I need a question id of checked answer as well to be passed to the 2nd php file

I used session but it only get the id of 1st checked and not for the others.

The 2nd php file name is answers.php for the time being i just want to post and get all values and session in answers.php file.

the main problem is with $_SESSION ['qid'] = $id; session

Note: i have used unset and destroy session to free the session and tried to re start it but i am field to do so..

<?php

include ("connection.php");

// $mysql_row = '';

$query_run = null;
$test = $_SESSION ['id'];
var_dump ( $_SESSION ['id'] );

$query1 = "SELECT * FROM question where testid = '" . $_SESSION ['id'] . "'";

if ($query_run = mysql_query ( $query1 )) {

    if (mysql_num_rows ( $query_run ) == null) {

        print "No result";

    } else {

        // echo'<form action="ss.php" method="post">';

        while ( $mysql_row = mysql_fetch_assoc ( $query_run ) ) {
            $id = $mysql_row ['qid'];
            $_SESSION ['qid'] = $id;
            echo ' <div class="panel-heading" style="background: black; font-weight:bold;">Question </div>';
            $data = $mysql_row ['questions'];
            ?>
<form>
                <br>
                <div class=" form-control well well-sm">

  <?php echo'<div style="font-weight:bold;"> Q: '.$data.' </div> '; ?>

<br>
                </div>


<?php

            $query1 = "SELECT * FROM `answer` where id='" . $id . "'";

            if ($query_run1 = mysql_query ( $query1 )) {
                if (mysql_num_rows ( $query_run ) == null) {

                } else {
                    while ( $mysql_row1 = mysql_fetch_assoc ( $query_run1 ) ) {

                        $data1 = $mysql_row1 ['ans1'];
                        $data2 = $mysql_row1 ['ans2'];
                        $data3 = $mysql_row1 ['ans3'];
                        $data4 = $mysql_row1 ['ans4'];

                        echo "\n";

                        ?>
                        <?php
                        echo '<div class="panel-heading" style="font-weight:bold;">Option1</div>';
                        ?>
<div class="form-control ">

                       <?php
                        echo "<input type='radio' value='$data1' name='opt1' onclick='OnChangeRadio (this)'> $data1<br />";

                        ?>

<br>
                </div>
                           <?php
                        echo '<div class="panel-heading" style="font-weight:bold;">Option2</div>';
                        ?>
                       <div class="form-control ">

                       <?php

                        echo "<input type='radio' value='$data2' name='opt1' onclick='OnChangeRadio (this)'> $data2<br />";

                        ?>


                       </div>
                             <?php
                        echo '<div class="panel-heading" style="font-weight:bold;">Option3</div>';
                        ?>
                       <div class="form-control ">

                       <?php

                        echo "<input type='radio' value='$data3' name='opt1' onclick='OnChangeRadio (this)'> $data3<br />";

                        ?>


                       </div>
                            <?php
                        echo '<div class="panel-heading" style="font-weight:bold;">Option4</div>';
                        ?>
                       <div class="form-control ">

                       <?php

                        echo "<input type='radio' value='$data4' name='opt1' onclick='OnChangeRadio (this)'> $data4<br />";
                        // echo'</form>';
                        ?>


                       </div>
<?php
                        echo '</form>';
                    }
                }
            }

            // $view_id1 = $view_id;

            // echo $view_id1;

        }
    }

} else {
    print mysql_error ();
}

// unset($_SESSION['qid']);

?>

connection.php

<?php 
session_start (); 
if (! $_SESSION ['user']) { 
    header ( "Location: index.php" ); 
    // redirect to main page to secure the welcome 
    // page without login access. 
} 
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Afaq Ahmad
  • 133
  • 1
  • 10
  • Should we assume that you have a `session_start()` in your included `connection.php` script??? – RiggsFolly Sep 07 '16 at 10:43
  • Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in _meow_ code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Sep 07 '16 at 10:44
  • the above is the code i have used at the top of my file which is uploaded already – Afaq Ahmad Sep 07 '16 at 10:47
  • 1
    In future to add info to your question, use the edit link and add the info into the question. Nobody can read code in a comment – RiggsFolly Sep 07 '16 at 10:49
  • okay i will thank you for the suggestion. – Afaq Ahmad Sep 07 '16 at 10:56
  • Looks like we would need the javascript(ajax) and also the `answer.php` to be able to make any suggestion on this. Except, if you `unset($_SESSION['qid']);` in thsi script it wont be available in `answers.php` – RiggsFolly Sep 07 '16 at 10:59

0 Answers0