How can HTTP requests be used to bridge the gap between JavaScript and PHP in a web application?

HTTP requests can be used to send data between JavaScript and PHP in a web application. By making AJAX requests from JavaScript to a PHP script, you can pass data back and forth seamlessly. This allows for dynamic content updates without needing to reload the entire page.

// PHP script to handle AJAX requests
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $data = json_decode(file_get_contents('php://input'), true);
    
    // Process the data
    $response = ['message' => 'Data received: ' . $data['message']];
    
    // Send back a JSON response
    header('Content-Type: application/json');
    echo json_encode($response);
}