How can AJAX be used to send data from JavaScript to a PHP script for processing?

To send data from JavaScript to a PHP script for processing, AJAX can be used. This involves making an asynchronous HTTP request from JavaScript to the PHP script, sending the data as parameters in the request. The PHP script then processes the data and returns a response, which can be handled by the JavaScript function.

<?php
// Retrieve the data sent from JavaScript
$data = $_POST['data'];

// Process the data as needed
// For example, you can perform database operations or calculations

// Return a response (e.g., JSON format)
$response = array('message' => 'Data processed successfully');
echo json_encode($response);
?>