-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddCategory.php
37 lines (32 loc) · 1.43 KB
/
addCategory.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
// addCategories.php
if ($_SERVER["REQUEST_METHOD"] === "POST") {
// Check if the action is to add a category
if (isset($_POST['action']) && $_POST['action'] === 'addCategory') {
$newCategory = $_POST['category'];
if ($newCategory) {
// Read existing categories
$jsonFile = 'categories.json';
$jsonData = file_get_contents($jsonFile);
$data = json_decode($jsonData, true);
// Check if the category already exists
if (!in_array($newCategory, $data['categories'])) {
$data['categories'][] = $newCategory;
// Save the modified data back to the JSON file
file_put_contents($jsonFile, json_encode($data, JSON_PRETTY_PRINT));
// Send a JSON response indicating success
echo json_encode(['success' => true, 'categories' => $data['categories']]);
exit();
} else {
// Send a JSON response indicating that the category already exists
echo json_encode(['success' => false, 'message' => 'Category already exists.']);
exit();
}
} else {
// Send a JSON response indicating that the category field is empty
echo json_encode(['success' => false, 'message' => 'Please fill in the category field.']);
exit();
}
}
}
?>