How can you determine which button was clicked in an HTML form using PHP?
To determine which button was clicked in an HTML form using PHP, you can give each button a unique name attribute and check which button's name was submitted in the form data. This can be achieved by checking the value of the button in the $_POST or $_GET superglobal arrays.
<form method="post">
<button type="submit" name="button1" value="Button 1">Button 1</button>
<button type="submit" name="button2" value="Button 2">Button 2</button>
</form>
<?php
if(isset($_POST['button1'])) {
echo "Button 1 was clicked";
} elseif(isset($_POST['button2'])) {
echo "Button 2 was clicked";
}
?>
Related questions
-
PHP user authentication email login
-
PHP sessions dynamic includes website sections
-
PHP form submissions empty results