How can AJAX be used to execute a PHP function when a button is clicked within a form?

To execute a PHP function when a button is clicked within a form using AJAX, you can use JavaScript to send an AJAX request to a PHP file that contains the function you want to execute. The PHP file will process the request and return the result back to the JavaScript function. This way, you can perform server-side operations without refreshing the page.

```php
// HTML form with a button
<form id="myForm">
    <button id="myButton">Click Me</button>
</form>

// JavaScript code to handle button click event and send AJAX request
<script>
    document.getElementById('myButton').addEventListener('click', function() {
        var xhr = new XMLHttpRequest();
        xhr.open('POST', 'myphpfile.php', true);
        xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4 && xhr.status == 200) {
                console.log(xhr.responseText); // Response from PHP function
            }
        };
        xhr.send();
    });
</script>
```

In the PHP file (myphpfile.php), you can write the PHP function that you want to execute when the button is clicked. The function will be triggered when the AJAX request is sent from the JavaScript code above.