Код: Выделить всё
if ($requestMethod === 'POST') {
// Get the raw POST data
$data = json_decode(file_get_contents('php://input'), true);
// Check if all required fields are present
if (isset($data['server_id']) && isset($data['username']) && isset($data['email']) && isset($data['date'])) {
$server_id = strval($data['server_id']);
$username = $data['username'];
$email = $data['email'];
$date = $data['date'];
$filePath = __DIR__ . '/emails.json';
if (file_exists($filePath)) {
$jsonData = json_decode(file_get_contents($filePath), true);
} else {
$jsonData = [];
}
// Initialize the server_id array if not present
if (!isset($jsonData[$server_id])) {
$jsonData[$server_id] = [];
}
// Add the new data to the server_id array
$jsonData[$server_id][] = [
'username' => $username,
'email' => $email,
'date' => $date
];
file_put_contents($filePath, json_encode($jsonData, JSON_PRETTY_PRINT));
echo json_encode(['status' => 'success', 'message' => 'Data saved successfully']);
} else {
echo json_encode(['status' => 'error', 'message' => 'Invalid input']);
}
Это что у меня есть сейчас. Есть идеи, как это исправить?
Подробнее здесь: https://stackoverflow.com/questions/787 ... up-numbers