How can the execution of a PHP script be triggered only upon clicking on a specific element, such as a photo album, before opening a Fancybox gallery?

To trigger the execution of a PHP script only upon clicking on a specific element, such as a photo album, before opening a Fancybox gallery, you can use JavaScript to make an AJAX call to the PHP script when the element is clicked. This way, the PHP script will only be executed when the specific element is clicked.

// HTML element that triggers the PHP script
<div id="photoAlbum">Photo Album</div>

// JavaScript code to trigger PHP script on click
<script>
document.getElementById('photoAlbum').addEventListener('click', function() {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', 'script.php', true);
    xhr.send();
});
</script>