What are some examples of button code in PHP that can execute custom-defined functions?
To execute custom-defined functions in PHP when a button is clicked, you can use a form with a button input element and submit the form to a PHP script that calls the desired function. You can pass the function name as a parameter in the form submission and use that parameter to call the function dynamically in PHP. Example PHP code snippet:
<?php
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the function name from the form submission
$functionName = $_POST['functionName'];
// Call the custom-defined function based on the function name
if (function_exists($functionName)) {
$functionName();
}
}
// Custom-defined functions
function customFunction1() {
echo "Function 1 executed!";
}
function customFunction2() {
echo "Function 2 executed!";
}
?>
<form method="post">
<input type="hidden" name="functionName" value="customFunction1">
<button type="submit">Execute Function 1</button>
</form>
<form method="post">
<input type="hidden" name="functionName" value="customFunction2">
<button type="submit">Execute Function 2</button>
</form>