-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinish-meal.php
37 lines (32 loc) · 1.36 KB
/
finish-meal.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
session_start();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['meal_name'], $_POST['calories'], $_POST['fat'], $_POST['protein'], $_POST['carbohydrates'])) {
// Check if the custom_meal cookie exists
$previousCustomMeal = [];
if (isset($_COOKIE['custom_meal'])) {
$previousCustomMeal = json_decode($_COOKIE['custom_meal'], true);
}
// New custom meal data
$customMealData = [
'meal_name' => $_POST['meal_name'],
'calories' => $previousCustomMeal['calories'] + $_POST['calories'],
'fat' => $previousCustomMeal['fat'] + $_POST['fat'],
'protein' => $previousCustomMeal['protein'] + $_POST['protein'],
'carbohydrates' => $previousCustomMeal['carbohydrates'] + $_POST['carbohydrates'],
];
// Convert the array to JSON
$jsonData = json_encode($customMealData);
// Set a cookie to store the JSON data
setcookie('custom_meal', $jsonData, time() + (86400), '/'); // Cookie expires in 1 day
// Send the JSON data back as the response
header('Content-Type: application/json');
echo $jsonData;
header("Location: profile.php");
} else {
echo 'Error: Some data is missing.';
}
} else {
echo 'Invalid request method.';
}
?>