How can AJAX be used to send HTTP requests from JavaScript to a PHP server in the background?

AJAX can be used to send HTTP requests from JavaScript to a PHP server in the background by using the XMLHttpRequest object to create a new request, specifying the HTTP method (GET or POST), the URL of the PHP script to send the request to, and any data to be sent along with the request. The PHP script on the server side can then process the request, perform any necessary operations, and return a response back to the JavaScript client.

<?php
// Retrieve data sent via AJAX
$data = $_POST['data'];

// Process the data (e.g., save to database)
// Send a response back to the client
echo "Data received successfully!";
?>