How can PHP and JavaScript be effectively integrated to execute different commands based on button clicks?

To integrate PHP and JavaScript to execute different commands based on button clicks, you can use AJAX to send requests to a PHP script that processes the commands and returns the appropriate response. In the JavaScript code, you can use event listeners on the buttons to trigger the AJAX requests. The PHP script will then handle the logic based on the request parameters and return the desired output.

<?php
// process.php

if(isset($_POST['command'])) {
    $command = $_POST['command'];
    
    // Perform different actions based on the command
    if($command == 'action1') {
        // Execute command for action 1
        echo "Action 1 executed";
    } elseif($command == 'action2') {
        // Execute command for action 2
        echo "Action 2 executed";
    } else {
        echo "Invalid command";
    }
}
?>