2

I am trying to do an ajax post for an e-commerce site.

So what I did was do an ajax post, when user click to "Add to cart" it will do the ajax post to the following php page with the code

<?php
include 'inc/session.php';
        $product_id = $_POST['product_id'];
        $quantity = $_POST['quantity'];

if($_POST['action'] == "add") {

    $_SESSION[$product_id]['quantity'] = $quantity;
    $_SESSION["hello"] = "WORLD";
    //if add success return 111, else echo something else
    echo "111";
}
if($_POST['action'] == "remove") {

        $_SESSION[$product_id]['quantity'] = $quantity;
    //if add success return 111, else echo something else
    echo "111";
}
?>

When I try to go to other page after the ajax post, I did

print_r($_SESSION);

But it return empty array.

Everything is done within the same domain, same server.

Thanks for helping!

Below is my session file

<?php
ini_set("session.cache_limiter", "must-revalidate");
session_name("sess1");
session_start();
?>

2 Answers2

1

At the top of the page you're posting to make sure you add:

session_start();

user4024288
  • 110
  • 4
1

Share your AJAX call.

Is is possible $_POST["action"] was never either "add" or "remove"? If that is the case then your $_SESSION array would not be written.

You can check that by modifying the $_SESSION out of the if statement.

fos.alex
  • 5,317
  • 4
  • 16
  • 18