I am new to Next.js and have been trying to connect to a cart session in a PHP file hosting at localhost/aits from the Next.js app which runs at localhost:3000
in the same folder localhost/aits.
For some reason, the fetch API I am using in the Next.js app to add data to the cart is actually not setting the session with data. When I console.log
the data session returned from the fetch API, I see the session set for the current product added but when I open a new browser to go to the cart URL, there are no products in it. Also when I add another product to the cart, it overrides the previous cart instead of adding to it or updating it.
It seems sessions do not work for fetch apis between localhost:3000
and a PHP file that resides in the same localhost folder as the Next.js app.
/* Next js file */
const ticketData = {
pageId: id,
discount: discount ? discount : 0
};
let fd = new FormData();
for(const name in ticketData) {
fd.append(name, ticketData[name]);
}
let resp = await fetch(`http://localhost/aits/add-to-cart.php`,{
method: 'POST',
credentials: 'same-origin', //I also used 'include' but same results
body: fd
})
let data = await resp.json()
console.log(data);
/* Add to cart PHP file */
$data = $_SESSION['kcart'];
function Exists($index, $array)
{
if (array_key_exists($index, $array))
{
return true;
}
else
{
return false;
}
}
if(Exists($pgName, $data['items'])) {
$data['items']["c<cms:show k_page_name />"]['quantity'] += $productCount;
$data['items']["c<cms:show k_page_name />"]['line_total'] += ($productCount * <cms:show pp_price />);
$subTotal = $data['sub_total'] + ($productCount * <cms:show pp_price />);
$discount = $data['discount'] + ($productCount * <cms:if discount gt '0'><cms:show discount /><cms:else/>0</cms:if>);
//print_r($data['items']["c<cms:show k_page_name />"]);
$action_status['status'] = 'ok';
$action_status['status_msg'] = 'Pdt updated';
} else {
$newItem = array
(
"line_id" => md5("c"."<cms:show k_page_name />"),
"id" => "<cms:show k_page_id />",
"name" => "<cms:show k_page_name />",
"title" => "<cms:show k_page_title />",
"link" => "<cms:show k_page_link />",
"price" => "<cms:show pp_price />",
"quantity" => "<cms:show qty />",
"line_total" => "<cms:mul qty pp_price />",
"skip_line_total" => "0",
"options" => "Array()",
"requires_shipping" => "1",
"product_thumb" => "<cms:show pp_product_thumb />",
"previous_price" => "<cms:show pp_previous_price />",
"discount_scale" => "",
"shipping_scale" => "",
"orig_price" => "<cms:show pp_price />",
"line_discount" => "0"
);
$data['items']['c'."<cms:show k_page_name />"] = $newItem;
}
My Answer:
It seems like the issue might be related to how you are handling sessions in your PHP file. Here are a few suggestions to troubleshoot and potentially fix the issue:
1. Make sure you are starting the session in your PHP file before trying to access or modify the session data. You can start the session by adding `session_start();` at the beginning of your PHP file.
2. Check if the session data is being properly set and updated in your PHP file. You can use `var_dump($_SESSION);` to print out the session data and see if it is being updated correctly.
3. Ensure that the session data is being saved properly after making changes. You can use `session_write_close();` to explicitly write the session data to the server.
4. Verify that the session cookie is being set correctly in the response from your PHP file. You can check the response headers in the Network tab of your browser's developer tools to see if the session cookie is being set.
5. Consider using a different approach to handle sessions, such as using JWT tokens or local storage, if the session handling in PHP continues to cause issues.
By following these suggestions and debugging the session handling in your PHP file, you should be able to resolve the issue with adding and updating the cart session data using the Fetch API in your Next.js app.
Rate this post
3 of 5 based on 1068 votesComments