How can a PHP function be triggered with a parameter by an input button?
To trigger a PHP function with a parameter by an input button, you can use a form with a hidden input field to pass the parameter value to the PHP script when the button is clicked. In the PHP script, you can retrieve the parameter value using the $_POST superglobal array and then call the function with the parameter.
<?php
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve the parameter value from the hidden input field
$param = $_POST['param'];
// Call the function with the parameter
yourFunction($param);
}
// Define the function
function yourFunction($param) {
// Your function code here
}
?>
<form method="post">
<input type="hidden" name="param" value="parameter_value">
<input type="submit" value="Trigger Function">
</form>
Keywords
Related Questions
- How can global variables be effectively used in PHP functions to manipulate arrays like $xml in the code snippet provided?
- How can a "Fatal error" be handled in PHP to trigger a specific action, such as sending an email with error details?
- What are the potential pitfalls to avoid when updating multiple records in a MySQL database using PHP?