How can a PHP developer handle multiple form submissions with different actions in a single form?
To handle multiple form submissions with different actions in a single form, you can use hidden input fields to identify which action the form should take. Based on the value of these hidden fields, you can execute different blocks of code in your PHP script.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST['action1'])) {
// Handle action 1
} elseif (isset($_POST['action2'])) {
// Handle action 2
} else {
// Handle default action
}
}
?>
<form method="post" action="">
<!-- Hidden input fields to identify actions -->
<input type="hidden" name="action1" value="action1">
<input type="hidden" name="action2" value="action2">
<!-- Other form fields -->
<input type="submit" value="Action 1">
<input type="submit" value="Action 2">
</form>