<?php
// Include the database connection file
require_once 'connection.php';

// Set header for JSON response
header('Content-Type: application/json');

// Initialize an array to store the response
$response = array();

// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Escape the groupId input for security (assuming connection is established)
    $groupId = $conn->real_escape_string($_POST['groupId']);

    // Attempt delete query execution
    $delete_sql = "DELETE FROM `grouptable` WHERE `groupId`='$groupId'";

    if ($conn->query($delete_sql) === TRUE) {
        if ($conn->affected_rows > 0) {
            // Group deleted successfully
            $response['status'] = "success";
            $response['message'] = "Group deleted successfully.";
        } else {
            // Group not found
            $response['status'] = "error";
            $response['message'] = "Group not found.";
        }
    } else {
        // Query error
        $response['status'] = "error";
        $response['message'] = "Error: " . $conn->error;
    }
} else {
    // If the request method is not POST
    $response['status'] = "error";
    $response['message'] = "Invalid request method.";
}

// Close the connection
$conn->close();

// Return the response as JSON
echo json_encode($response);
?>
