In HTML forms, you can have multiple submit buttons, each serving a different purpose or triggering a different action when clicked. Here's how you can implement multiple submit buttons within a single HTML form:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Multiple Submit Buttons Example</title> </head> <body> <form action="submit.php" method="post"> <!-- Input fields --> <label for="username">Username:</label> <input type="text" id="username" name="username"><br><br> <label for="password">Password:</label> <input type="password" id="password" name="password"><br><br> <!-- Submit buttons --> <button type="submit" name="action" value="save">Save</button> <button type="submit" name="action" value="update">Update</button> <button type="submit" name="action" value="delete">Delete</button> </form> </body> </html>
Form Element:
<form> tag wraps the input fields and submit buttons.Input Fields:
<input> elements for collecting user input (e.g., username and password).Submit Buttons:
<button> element inside the form has a type="submit" attribute, which makes it act as a submit button.name attribute on each button (name="action") allows you to differentiate which button was clicked when the form is submitted.value attribute specifies a unique value ("save", "update", "delete") associated with each button. This value will be sent to the server when the form is submitted.Form Action and Method:
action="submit.php": Specifies the URL or script that will handle the form submission.method="post": Specifies the HTTP method (POST in this case) used to send the form data to the server.On the server-side (e.g., in PHP), you can handle different actions based on the value of the clicked button:
<?php if ($_SERVER["REQUEST_METHOD"] == "POST") { $action = $_POST['action']; // Get the value of the clicked button switch ($action) { case 'save': // Handle save action break; case 'update': // Handle update action break; case 'delete': // Handle delete action break; default: // Handle default action or error break; } } ?> This setup allows you to have multiple submit buttons in a form, each triggering a different action when clicked and providing flexibility in handling user interactions.
How to create multiple submit buttons in an HTML form?
<button> or <input type="submit"> elements within a form, each with a different name or value.<form action="/submit" method="post"> <input type="text" name="input1" /> <button type="submit" name="submitButton" value="button1">Submit Button 1</button> <button type="submit" name="submitButton" value="button2">Submit Button 2</button> </form>
How to differentiate which submit button was clicked?
name attribute of the buttons to identify which button was clicked on the server-side.if ($_SERVER["REQUEST_METHOD"] == "POST") { if (isset($_POST['submitButton'])) { echo "You clicked: " . $_POST['submitButton']; } } How to handle multiple submit buttons in JavaScript?
<form id="myForm"> <input type="text" name="input1" /> <button type="button" onclick="handleSubmit('button1')">Submit Button 1</button> <button type="button" onclick="handleSubmit('button2')">Submit Button 2</button> </form> <script> function handleSubmit(button) { alert("You clicked: " + button); document.getElementById("myForm").submit(); } </script> Can I use different actions for different submit buttons?
<form id="myForm" action="/default" method="post"> <input type="text" name="input1" /> <button type="button" onclick="changeAction('/action1')">Submit Button 1</button> <button type="button" onclick="changeAction('/action2')">Submit Button 2</button> </form> <script> function changeAction(action) { document.getElementById("myForm").action = action; document.getElementById("myForm").submit(); } </script> How to style multiple submit buttons differently?
<style> .btn1 { background-color: blue; color: white; } .btn2 { background-color: green; color: white; } </style> <form action="/submit" method="post"> <button type="submit" class="btn1" name="submitButton" value="button1">Submit Button 1</button> <button type="submit" class="btn2" name="submitButton" value="button2">Submit Button 2</button> </form> How to add confirmation dialog for multiple submit buttons?
<form action="/submit" method="post"> <input type="text" name="input1" /> <button type="button" onclick="confirmSubmit('button1')">Submit Button 1</button> <button type="button" onclick="confirmSubmit('button2')">Submit Button 2</button> </form> <script> function confirmSubmit(button) { if (confirm("Are you sure you want to submit " + button + "?")) { document.forms[0].submit(); } } </script> How to prevent default action for multiple submit buttons?
<form id="myForm"> <input type="text" name="input1" /> <button type="submit" onclick="event.preventDefault(); handleSubmit('button1')">Submit Button 1</button> <button type="submit" onclick="event.preventDefault(); handleSubmit('button2')">Submit Button 2</button> </form> How to use AJAX with multiple submit buttons?
<form id="myForm"> <input type="text" name="input1" /> <button type="button" onclick="submitForm('button1')">Submit Button 1</button> <button type="button" onclick="submitForm('button2')">Submit Button 2</button> </form> <script> function submitForm(button) { const formData = new FormData(document.getElementById("myForm")); formData.append("submitButton", button); fetch('/submit', { method: 'POST', body: formData }).then(response => response.text()).then(data => console.log(data)); } </script> How to reset form fields after submitting with multiple buttons?
<form id="myForm" onsubmit="resetForm()"> <input type="text" name="input1" /> <button type="submit" name="submitButton" value="button1">Submit Button 1</button> <button type="submit" name="submitButton" value="button2">Submit Button 2</button> </form> <script> function resetForm() { document.getElementById("myForm").reset(); } </script> How to pass additional data with multiple submit buttons?
<form action="/submit" method="post"> <input type="text" name="input1" /> <input type="hidden" name="extraData" value="someValue" /> <button type="submit" name="submitButton" value="button1">Submit Button 1</button> <button type="submit" name="submitButton" value="button2">Submit Button 2</button> </form>
pyinstaller scala listviewitem camelcasing angular-material-7 core-data office365api fuzzy-search countdown android-app-bundle