How can PHP be integrated with JavaScript to avoid inline code in HTML?

To avoid inline code in HTML, PHP can be integrated with JavaScript by using AJAX (Asynchronous JavaScript and XML) to make server-side requests and dynamically update the webpage without refreshing. This helps separate the presentation layer (HTML) from the logic (PHP) and behavior (JavaScript), resulting in cleaner and more maintainable code.

<?php
// PHP code to handle AJAX request
if(isset($_POST['data'])){
    $data = $_POST['data'];
    
    // Process data
    
    // Return response
    echo json_encode(['message' => 'Data processed successfully']);
}
?>