How can JavaScript be integrated with PHP to enhance form functionality, such as adding content with buttons?

To enhance form functionality by adding content with buttons, JavaScript can be integrated with PHP by using AJAX. This allows for asynchronous communication between the client-side JavaScript and server-side PHP, enabling dynamic content updates without refreshing the page. By sending requests to the server using JavaScript, PHP can process the data and return a response to update the content on the webpage.

<?php
// PHP code to handle the AJAX request
if(isset($_POST['data'])) {
    $data = $_POST['data'];
    
    // Process the data, for example, add content based on the input
    
    // Return the updated content as a response
    echo "Content added: " . $data;
}
?>