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

danimohamadnejad's avatar

session variable is not updating in ajax request

hello. in product details page I have a form by which I can add the product to cart through ajax. cart date are stored in a session variable . ajax request looks not updating session variable. I tested using ordinary http request and redirected to another route after setting session variable and everything is okay. however ajax request cannot update session variable. please help

0 likes
4 replies
Nakov's avatar

It will be better if you can show the code on what you are trying to do.

1 like
danimohamadnejad's avatar

please consider follwing form:

I send above form using ajax request. I also can verify that all data reaches server. I just do following:

session(['cart_product'=>$data['productId']]);

session(['cart_quantity'=>$data['quantity']]);

above session variables simply set when I send form using traditional http because I can get them in another page. but ajax request doesnt set them.

Nakov's avatar
Nakov
Best Answer
Level 73

There are couple of things that I cannot say because I cannot see your code.

So is the route that you are hitting an API route or a normal one defined in your web.php? Because if it is an API there won't be a session.

Then how do you test? Do you check your browser's network tab to make sure that the request reaches the server side and it returns successful? Also that your $data on the server contains the data?

1 like

Please or to participate in this conversation.