2016-08-31 12 views
-1

次のページを作成しました。 1. questions.php 2のfunctions.php questions.phpフォーム上の質問フォームがのfunctions.phpに提出されたとき、私は、ページfunction.phpのセッション変数の値を設定しているセッション変数から値を取得できません。

3. result.phpコードが実行され、私はresult.phpページにセッション変数の値をエコーし​​ます。しかし、私はセッション値result.theセッション値を得ることができません空の結果です。anyoneヘルプしてください。

 functions.php 

    if(isset($_POST['question_form'])) 
    { 
    global $conn; 
    if (!isset($_SESSION['result'])) { 
    $result = 0; 
    $_SESSION['result'] = 0; 
    } 
    if (!isset($_SESSION['attempted'])) { 
    $attempted = 0; 
    $_SESSION['attempted'] = 0; 
    } 
    $resArray = array(); 
    $resArray['message'] = ''; 
    $resArray['status'] = ''; 
    $no = $_POST['no']; 
    $postedAnswer = $_POST['answer_'.$no]; 
    $question_id = $_POST['question_id']; 
    $subject_id = $_POST['subject_id']; 

    $sql = "SELECT True_answer FROM question WHERE QuestionId = '$question_id' AND SubjectId = '$subject_id'"; 
    $result = mysqli_query($conn, $sql); 
    $row = mysqli_fetch_assoc($result); 

    $True_answer = $row['True_answer']; 

    if($postedAnswer === $True_answer) 
    { 

     $result = $_SESSION['result']; 
     $result++; 
     $_SESSION['result'] = $result; 
    } 
     $attempted = $_SESSION['attempted']; 
     $attempted++; 
     $_SESSION['attempted'] = $attempted; 
    $resArray['status'] = true; 
    //$resArray['q_id'] = $no; 
    $resArray['message'] = 'Submitted Successfully'; 
    echo json_encode($resArray); 
    exit(); 
} 

===== questions.php ========

 <form action="includes/functions.php" id="question_form_<?php echo $j; ?>" > 
            <div class='container' > 
            <div class='row'> 
            <div class='col-lg-12'> 
     <div class='thumbnail'> 

    <p id="question_description">Q.<?php echo $i; ?><br><?php echo $row['QuestionDescription']; ?></p> 
     <div class="questions_options"> 
     <label><input type="radio" id="btn_radio" name="answer_<?php echo $j; ?>" value="<?php echo $row['Option1']; ?>"><?php echo $row['Option1']; ?></input></label><br> 


     <label><input type="radio" id="btn_radio" name="answer_<?php echo $j; ?>" value="<?php echo $row['Option2']; ?>"><?php echo $row['Option2']; ?></input></label><br> 

     <label><input type="radio" id="btn_radio" name="answer_<?php echo $j; ?>" value="<?php echo $row['Option3']; ?>"><?php echo $row['Option3']; ?></input></label><br> 

     <label><input type="radio" id="btn_radio" name="answer_<?php echo $j; ?>" value="<?php echo $row['Option4']; ?>"><?php echo $row['Option4']; ?></input></label><br><br> 
       <input type="hidden" name="question_id" value="<?php echo $row['QuestionId'] ?>"> 
       <input type="hidden" name="subject_id" value="<?php echo $row['SubjectId'] ?>"> 
       <input type="hidden" name="question_form" value="question_form"> 
       <input type="hidden" name="no" value="<?php echo $j; ?>"> 
       <button class="btn btn-primary btn-sm" id="btn_submit">Submit<i class="glyphicon glyphicon-arrow-right" > 

       </i></button>  
     </div> 



    </div> 
    </div> 

    </div> 

    </div> 

    </form> 

============ result.php ===== ==========

<table class="table table-striped table-hover" id="result_table" > 

    <thead> 
     <tr> 
     <th style="text-align: center;">Total Questions</th> 
     <th style="text-align: center;">Attempted Questions</th> 
     <th style="text-align: center;">Total Marks</th> 
     <th style="text-align: center;">Obtained Marks</th> 
     </tr> 
    </thead> 
    <tbody> 
    <tr> 
     <td style="text-align: center;"><?php echo $number_of_questions;?></td> 
     <td style="text-align: center;"><?php echo $_SESSION['attempted']; ?></td> 
     <td style="text-align: center;"><?php if(!isset($_SESSION['result'])) 
     { 
     echo "empty"; 
     } 
     else 
     { 
     echo $_SESSION['result']; 
     } 
     ?></td> 
     <td style="text-align: center;"><?php echo $marks_obtained;?></td> 
    </tr> 
    </tbody> 
    </table> 
+0

を使用してセッションを開始してください)(SESSION_STARTを忘れてしまいました。セッション変数が使用されている場合は、ページの先頭にこれを追加してください。 – JYoThI

+0

各ページでsession_start()関数を起動しましたが、目的の結果が得られません。 –

答えて

0

のsession_start()関数

+0

はい、私はすでに各ページでsession_start()関数を起動していますが、まだ動作しません。 sujivasagam –

+0

あなたは$ resultのようなあなたの結果を得ましたか?それらを印刷してチェックしましたか? – sujivasagam

+0

はいresult.phpページで私の結果をチェックしましたが、reuslt.phpにif-else関数を置いているので、asnwerは "空"になります。 –

関連する問題