What is the role of HTML forms in PHP button detection?
HTML forms are essential for capturing user input, including button clicks, which can then be processed by PHP scripts. To detect which button was clicked in a form, you can use the 'name' attribute on the button elements and check for the corresponding value in the $_POST or $_GET superglobals in your PHP script.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST['submit_button'])) {
// Code to handle when the submit button is clicked
} elseif (isset($_POST['cancel_button'])) {
// Code to handle when the cancel button is clicked
}
}
?>
<form method="post" action="">
<button type="submit" name="submit_button" value="submit">Submit</button>
<button type="submit" name="cancel_button" value="cancel">Cancel</button>
</form>