What are the best practices for handling onclick functions in PHP files?

When handling onclick functions in PHP files, it is important to separate the PHP logic from the HTML markup to maintain clean and organized code. One way to achieve this is by using AJAX to send requests to a PHP file that processes the onclick function. This allows for better separation of concerns and improves code readability.

// HTML file with onclick function
<button onclick="sendRequest()">Click me</button>

// JavaScript function to send AJAX request
function sendRequest() {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', 'process.php', true);
    xhr.send();
}

// PHP file (process.php) to handle the onclick function
<?php
// Process the onclick function here
?>