AJAX请求不使用外部页面?

I've been sitting here for a solid two hours trying to figure out why this won't work, so I turn to you all for some guidance.

I have the following script which pulls all the checkboxes that are currently checked on the page and sets their title attribute to the names array. Each title is a letter a-e depending on where in their particular section they are. This probably isn't the best way to do it, but due to previous coding constraints it was the easiest solution.

names = [];
$('input:checked').each(function() 
    {
         names.push($(this).attr("title"));
    });

question_one_val = names[0].toString();

    $.post("ajax_request.php", 
{first_question: question_one_val
 });

So then this is sent to my ajax page below:

session_start(); 
require_once 'question_responses.php';
require_once 'questions.php';




$test = $_POST['first_question'];

 $test = question_one($test);

 $_SESSION['views']=$test;

I'll also include a small snippet from the two require pages.

question.php

function question_one($answer)
    {
        $question_one_response="";
        if($answer == "a")
            {
                $question_one_response = $question_one_response_a;
            }
        if($answer == "b")
            {
                $question_one_response = $question_one_response_b;
            }

        if($answer == "c")
            {
                $question_one_response = $question_one_response_c;
            }

        if($answer == "d")
            {
                $question_one_response = $question_one_response_d;
            }

        if($answer == "e")
            {
                $question_one_response = $question_one_response_e;
            }

            return $question_one_response;
    }

and question_responses.php

$question_one_response_a = "Response a";
$question_one_response_a = "Response b";
$question_one_response_a = "Response c";
$question_one_response_a = "Response d";

Now as of right now, when I try to access the session variable I've either gotten a blank string, or the variable isn't set.

What really bugs me is I've done some tests without trying to set it to the response. So:

$test = $_POST['first_question'];


     $_SESSION['views']=$test;

Would return the proper a-e value on the page I'm displaying it on. So my ajax request, along with the javascript to get the value, seems to be working, and I've figured it must be a problem with the other pages, but I'm having trouble figuring out what exactly is the cause.

So if I'm not wrong, you already checked all that points ?

  • session_start(); is always setted and at the top of your page, before any output (even a blank char).

  • question_one_val contains the expected value (with console.log())

  • $test contains the expected value (with a var_dump)

  • question_one returns the expected value <= your problem may be there, are you sure that $answer matches at least one of the conditions ?

You should try to modify $question_one_response=""; by $question_one_response="test"; and see if your session contains a test string !