How can AJAX be used to pass variables from HTML to PHP without reloading the page?

To pass variables from HTML to PHP without reloading the page, AJAX can be used to send an asynchronous request to a PHP script with the data. This allows the PHP script to process the variables without the need to reload the entire page. The PHP script can then send a response back to the HTML page with the processed data.

<?php
if(isset($_POST['variable_name'])){
    $variable = $_POST['variable_name'];
    
    // Process the variable as needed
    
    // Send a response back to the HTML page
    echo $processed_data;
}
?>