Two Submit Buttons in One Form — How to Know Which Was Clicked

Stephen NdegwaStephen Ndegwa
·
2 min read
Two Submit Buttons in One Form — How to Know Which Was Clicked

Introduction

Many web apps provide multiple submit actions within one form — think:

  • Save
  • Publish
  • Delete
  • Continue
  • Submit & Next

The challenge:
When the form is submitted, how does your server know which button triggered the submission?

Luckily, HTML already solves this cleanly. You just need to use the right naming patterns.


The Correct HTML Pattern

Here’s the simplest and most universal approach:

<form method="post">
    <input type="submit" name="action" value="save" />
    <input type="submit" name="action" value="publish" />
</form>

When the user clicks one of the buttons, only that button’s name=value pair is included in the POST data.

Example POST data:

  • If “Save” was clicked → action=save
  • If “Publish” was clicked → action=publish

This works in PHP, Python (Flask/Django), Node.js, Java, Ruby, Go, .NET, and more.


Serverside Examples

PHP

if ($_POST['action'] === 'save') {
    // Save draft logic
}

if ($_POST['action'] === 'publish') {
    // Publish logic
}

Python (Flask)

action = request.form.get("action")

if action == "save":
    # save draft
elif action == "publish":
    # publish

Node.js (Express)

const action = req.body.action;

if (action === "save") {
    // save draft
}
if (action === "publish") {
    // publish
}

Ruby on Rails

case params[:action]
when "save"
  # save draft
when "publish"
  # publish
end

Alternate Patterns

1. Using Different Names

<input type="submit" name="save_btn" value="Save">
<input type="submit" name="publish_btn" value="Publish">

Server-side, check which field is present.

2. Using <button> with inner text

<button type="submit" name="action" value="save">Save</button>
<button type="submit" name="action" value="publish">Publish</button>

3. Using FormData + JavaScript Custom Submission

Useful for advanced cases, like AJAX.


⚠️ Common Mistakes to Avoid

  • Using two submit buttons without names — the server won’t know which was clicked.
  • Using JavaScript to guess the clicked button — unnecessary and brittle.
  • Hoping the submit text (label) gets sent — it doesn’t.

Best Practice (TL;DR)

Always give submit buttons:

  • the same name
  • unique value

Example:

<input type="submit" name="action" value="save">
<input type="submit" name="action" value="publish">

Done. Clean. Cross-platform. Works everywhere.


🏁 Conclusion

Handling multiple submit buttons is simple once you use HTML as intended. By giving each button the same name but different values (or unique names), the server will always know exactly which action the user triggered — without extra JavaScript or hacks.

This approach has been used reliably for more than 15 years and remains the cleanest solution today.

Share:

Related Guides

Automating JNLP Downloads with PowerShell Using Session Cookies

When managing remote servers or BMC interfaces, some resources such as JNLP (Java Network Launch Protocol) files require authentication via cookies and session handling. Manually downloading these files can be cumbersome. PowerShell provides a way to automate this process using web sessions and cookie management. Creating a Persistent Web Session A web session in PowerShell [&hellip;]

Stephen Ndegwa
·

Complete Guide to Downloading Files with PowerShell

Introduction PowerShell provides powerful tools for downloading files from web servers, with Invoke-WebRequest being the primary cmdlet for making HTTP requests. This guide covers everything from basic downloads to advanced scenarios involving authentication, cookies, and custom headers. Basic File Downloads Simple Download The most straightforward way to download a file: Download with Progress Bar PowerShell [&hellip;]

Stephen Ndegwa
·

The Complete Guide to Installing StorCLI on Linux and Windows

StorCLI (Storage Command Line Tool) is Broadcom&#8217;s powerful command-line utility for managing LSI MegaRAID and PRAID controllers. Whether you&#8217;re managing hardware RAID arrays on servers or workstations, StorCLI provides comprehensive control over your storage infrastructure. This guide will walk you through the complete installation process on both Linux and Windows systems. What is StorCLI? StorCLI [&hellip;]

Stephen Ndegwa
·