How can PHP be used to determine which button was clicked in an HTML form and execute the corresponding database function?
To determine which button was clicked in an HTML form and execute the corresponding database function in PHP, you can use the `$_POST` superglobal array to check which button was submitted. You can assign unique values to each button using the `name` attribute and then check which button was clicked using an `if` statement. Based on the button clicked, you can call the corresponding database function to execute.
if(isset($_POST['button1'])){
// Call function for button1
// Execute corresponding database function
} elseif(isset($_POST['button2'])){
// Call function for button2
// Execute corresponding database function
}
// HTML form with buttons
<form method="post">
<button type="submit" name="button1" value="button1">Button 1</button>
<button type="submit" name="button2" value="button2">Button 2</button>
</form>
Related Questions
- What are the advantages of assigning values to radio buttons in PHP forms and how does it impact data processing?
- Are there any potential pitfalls or security concerns when storing objects in PHP sessions?
- What PHP functions or methods are recommended for managing user input and file handling in a forum setting?