false, "message" => ""]; try { if ($_SERVER["REQUEST_METHOD"] !== "POST") { throw new Exception("Invalid request method."); } // Recupera i dati dal body JSON (non da $_POST direttamente) $input = json_decode(file_get_contents('php://input'), true); $template_id = isset($input['template_id']) ? intval($input['template_id']) : null; $schemajson = isset($input['schemajson']) ? trim($input['schemajson']) : ''; // Controllo sui campi obbligatori if (empty($template_id) || empty($schemajson)) { throw new Exception("All fields marked with * are required, including schemajson."); } // Validazione del JSON $decoded_json = json_decode($schemajson, true); if (json_last_error() !== JSON_ERROR_NONE) { throw new Exception("Invalid JSON format for schemajson."); } // Connessione al database $db = DBHandlerSelect::getInstance(); $pdo = $db->getConnection(); // Aggiorna il database $stmt = $pdo->prepare("UPDATE excel_templates SET schemajson = ?, updated_at = NOW() WHERE id = ?"); $stmt->execute([ $schemajson, $template_id ]); if ($stmt->rowCount() > 0) { $response["message"] = "Schema JSON updated successfully!"; } else { $response["message"] = "No changes detected, but operation completed."; } $response["success"] = true; } catch (Exception $e) { $response["message"] = $e->getMessage(); } // Restituisce un JSON per il fetch echo json_encode($response);