How can POST data be passed to a PHP file without using an additional form?

To pass POST data to a PHP file without using an additional form, you can use AJAX to send an HTTP request with the data to the PHP file. This allows you to send data to a PHP file in the background without needing a form submission.

<?php
// Check if POST data is received
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Get the POST data
    $data = json_decode(file_get_contents('php://input'), true);

    // Process the data
    // For example, you can access the data like $data['key']

    // Send a response
    echo json_encode(['message' => 'Data received successfully']);
}
?>