How can a PHP function be triggered by a button in a separate PHP file?

To trigger a PHP function by a button in a separate PHP file, you can use AJAX to send a request to the PHP file containing the function. When the button is clicked, an AJAX call is made to the PHP file, which then executes the desired function. This allows for the function to be triggered without reloading the page.

// Button in separate PHP file
<button id="triggerFunction">Trigger Function</button>

<script>
document.getElementById("triggerFunction").addEventListener("click", function() {
  var xhr = new XMLHttpRequest();
  xhr.open("GET", "function.php", true);
  xhr.send();
});
</script>
```

```php
// function.php
<?php

// Function to be triggered
function myFunction() {
  // Function code here
}

// Check if AJAX request is made
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
  myFunction();
}

?>