How can PHP functions be executed only upon clicking a link in HTML?
To execute PHP functions only upon clicking a link in HTML, you can use AJAX to send a request to a PHP script when the link is clicked. This way, the PHP functions will be executed without refreshing the page. You can set up a click event listener on the link in JavaScript and use the fetch API to send an AJAX request to the PHP script.
<?php
if(isset($_POST['data'])){
// Perform PHP functions here
echo "PHP functions executed successfully!";
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Execute PHP Functions on Link Click</title>
</head>
<body>
<a href="#" id="execute-php">Click to Execute PHP Functions</a>
<script>
document.getElementById('execute-php').addEventListener('click', function() {
fetch('your_php_script.php', {
method: 'POST',
body: new URLSearchParams({
data: 'execute'
})
})
.then(response => response.text())
.then(data => {
console.log(data); // Output response from PHP script
});
});
</script>
</body>
</html>