Noby Fujioka
Noby Fujioka

Reputation: 1824

php not remembering variable when I use jQuery ajax

I have an issue with php not remembering variable when I use jQuery ajax. Could you help me how I can make it remember its variables?

I have code files as attached at the bottom of this message. First, question.php sends a question to javascript (via jQuery ajax) and display in html. question.php file also do the sum and put it in the variable $correctAnsweror$_SESSION[correct_answer]. Then, user types in his answer in html and javascript sends the answer to .check_answer.php (via jQuery ajax). After that, php code in check_answer.php file compares the user answer with the variable $correct_answer or $_SESSION[correct_answer] to see if the answer is correct.

Problem is that the check_answer.php does not seem to remember the variable $correct_answer set in question.php, so it cannot correctly check the answer. I tried to use $_SESSION[correct-answer] instead, but still the server side (php) does not remember the value inside $correct_answer or &SESSION[correct_answer].

Could you tell me what I am doing wrong here and how I can make php remember the variable for the correct answer?

Thank you, Noby

index.php

<html>
    <head>
        <meta charset="UTF-8">
        <title>Ajax-powered Quiz</title>


    </head>
    <body>
        <p> Answer the following question. </p>
        <p id="question_text">Question</p>
        <p id="response_text">Response</p>

        <input id="your_answer" type="textbox">    
        <input id="submit_button" type="button" value="submit">

    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="js/main.js"></script> 
    </body>
</html>

main.js

$.ajax({
        url:"php/question.php"
}).done(function(data){
    $("#question_text").html(data);
});


function server_response(data) {
    $("#response_text").html(data);
}

function submit_answer() {
    alert("answer submitted!");
    var your_answer;
    your_answer = parseInt( $("#your_answer").val() );

    var send_object = {
        yourAns: your_answer
    };

    //$.get("php/check_answer.php", send_object, server_response);

  $.ajax ({
    url: 'php/check_answer.php',
    type: 'post',
    data: send_object,
    success: server_response
  });
}



$("#submit_button").click(submit_answer);

question.php

<?php
session_start();

$numb1 = 2;
$numb2 = 3;
$_SESSION['correct_answer'] = $numb1 + $numb2;

echo "$numb1 + $numb2 = ?";

?>

check_answer.php

<?php

$your_answer = filter_input(INPUT_POST, "yourAns");
echo "your answer is $your_answer!";

$correct_answer = $_SESSION['correct_answer'];
if ($your_answer == $correct_answer) {
    echo "You are correct!";
} else {
    echo "You are wrong!";
    echo "Correct answer is $correct_answer!";
}



?>

Upvotes: 0

Views: 65

Answers (2)

Niranjan N Raju
Niranjan N Raju

Reputation: 11987

You are not starting your session in check_answer.php.

so add session_start() in your check_answer.php. It will work:)

Upvotes: 1

Altaf Hussain
Altaf Hussain

Reputation: 5202

In your check_answer.php file, at top start session first with session_start() method. It will work then.

Upvotes: 1

Related Questions