Is it possible to directly call a function from a form in PHP?
Yes, it is possible to directly call a function from a form in PHP by specifying the function name in the action attribute of the form tag. When the form is submitted, the specified function will be called.
<?php
// Function to be called
function myFunction() {
// Function logic here
echo "Function called successfully!";
}
// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Call the function if form is submitted
if ($_POST["form_action"] == "myFunction") {
myFunction();
}
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<input type="hidden" name="form_action" value="myFunction">
<button type="submit">Call myFunction</button>
</form>
Keywords
Related Questions
- What are the potential pitfalls of using IP addresses for user authentication in PHP, especially considering users who may use proxies or have dynamic IP addresses?
- What are the best practices for limiting the display of long text content in PHP, such as showing only the first 50 words with a "read more" option?
- What is the syntax for using isset() with multiple variables in PHP?