How can you differentiate between different form submission buttons in PHP?
When working with form submission buttons in PHP, you can differentiate between different buttons by giving each button a unique name attribute and checking which button was clicked using the $_POST superglobal. This allows you to perform different actions based on which button was clicked.
<form method="post">
<button type="submit" name="button1">Button 1</button>
<button type="submit" name="button2">Button 2</button>
</form>
<?php
if(isset($_POST['button1'])) {
// Code to execute when Button 1 is clicked
} elseif(isset($_POST['button2'])) {
// Code to execute when Button 2 is clicked
}
?>
Keywords
Related Questions
- How can PHP developers ensure that the latest message in a shoutbox is displayed at the bottom instead of at the top?
- What are the best practices for handling form submissions and data processing in PHP scripts to avoid errors like incorrect timestamp storage?
- How can one determine if a variable is an array in PHP?