Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

habibjutt868's avatar

Laravel session variable remove after page load when set by ajax

I have a simple ajax code which saves the category and subcategory id in session. These two variables save in session but on next reload both values does not exist in session. This session issue is only with ajax. I have other points where i am storing and retrieving session and it is working file. Here is my ajax call.

    $("#search-select-category").on("click", function () {
    console.log("sending ajax");

    $.ajax({
        type: "POST",
        url: $("#select-category-search").prop('action'),
        data: {
            "_token": $("#select-category-search").find('input[name=_token]').val(),
            "category_id": $("input[name='category-search']").val(),
            "subcategory_id": $("input[name='subcategory-search']").val(),
        },
        success: function (response) {
            var response = JSON.parse(response);
            console.log(response.message);
            if(response.error) {
                console.log("error is here");
                $(".search-category-error span").text(response.message);
                $(".search-category-error").show();
            }
            else {
                console.log("selection is good");
                $(".search-category-error").slideDown();
                $(".select-category-modal").modal('hide');
            }
        }
    });
});

and the function that executes on ajax call is as follows

public function ajax_select_category_search(Request $request) {

    $error = false;
    $message = "";

    if (empty($request->category_id) || empty($request->subcategory_id)) {
        $error = true;
        $message = "Category or Sub Category is not selected";
    }

    if ($error == false) {
        session(['category_search' => $request->category_id]);
        session(['subcategory_search' => $request->subcategory_id]);
    }

    $response['error'] = $error;
    $response['message'] = $message;

    echo json_encode($response);
    die();
}

I have tried using file and database session but same problem in both cases. I am using laravel 5.3 Session class is also imported in the class i am using and there are no errors when i use this function.

0 likes
3 replies
jlrdw's avatar
jlrdw
Best Answer
Level 75

die(); ?????

2 likes
habibjutt868's avatar

die() is called in the php function that is called by ajax. I think this is not the problem. Laravel sets the session variables but on next reload both variables does not exit in session. I have also tried setting session variable with some unique names that is not used in my project just to make sure there is nothing in my project that is unsetting the session variable but it does not matter which variable name i use it does not exist in session on next reload.

habibjutt868's avatar

Hmm i removed the die() and it worked. Let me test it for some more cases.

2 likes

Please or to participate in this conversation.