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

// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Escape user inputs for security
    $sender_email = $conn->real_escape_string($_POST['sender_email']);
    $reciever_email = $conn->real_escape_string($_POST['reciever_email']);

    // Validate input (e.g., check if emails are valid)
    if (filter_var($sender_email, FILTER_VALIDATE_EMAIL) === false || filter_var($reciever_email, FILTER_VALIDATE_EMAIL) === false) {
        echo json_encode(array('error' => 'Invalid email format.'));
        exit();
    }

    // Attempt delete query execution
    $delete_sql = "DELETE FROM `requests` WHERE `sender_email` = '$sender_email' AND `reciever_email` = '$reciever_email'";

    if ($conn->query($delete_sql) === TRUE) {
        // Check if any rows were affected
        if ($conn->affected_rows > 0) {
            echo json_encode(array('success' => 'Request deleted successfully.'));
        } else {
            echo json_encode(array('error' => 'No matching request found to delete.'));
        }
    } else {
        echo json_encode(array('error' => 'Error: ' . $conn->error));
    }
}

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