Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9023853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:51:46+00:00 2026-06-16T05:51:46+00:00

Consider a simple form with 2 submit buttons <form method=post> <button type=submit name=command value=cancel>Cancel

  • 0

Consider a simple form with 2 submit buttons

<form method="post">
 <button type="submit" name="command" value="cancel">Cancel Order</button>
 <button type="submit" name="command" value="proceed">Save Order</button>
</form>

Once submitted, the server will know which submit button was used to submit the form by checking the value for command. Great!

In this case, I am using the onsubmit event handler of the form to preprocess and send the form data via AJAX. Like this: <form method="post" onsubmit="return ajaxSubmit(this);">

I’m using this ajaxSubmit function as a general way to check through any supplied form’s elements and send the data via Ajax instead. This works fine for determining the value of text fields, if checkboxes are “checked”, which radio is selected in a group, etc. The only thing it seems to get wrong (because I’m not sure how to check this) is which submit button was used to submit the form. i.e There is nothing in myForm["command"] to tell which of the 2 command buttons was actually used.

Instead, is there a way to access the same ‘post’ data that the server receives with JavaScript before it is sent?

Otherwise, is this just a flaw I need to work around? What’s the best way?

Edit:
Since all modern browsers will pass the name/value of the button used to submit the form (along with the other relevant parts like which option is selected from a group, checked checkbox name/values, etc.) can anyone explain why there is no way to access this data directly before it is sent to the server? Is there a reason why we shouldn’t be able to?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:51:48+00:00Added an answer on June 16, 2026 at 5:51 am

    Instead, is there a way to access the same ‘post’ data that the server receives with JavaScript before it is sent?

    Not that exact data, no.

    The usual way to know which submit button was pressed is (unfortunately) to attach click handlers to the buttons and have them set a variable (or the value of a hidden field), which you can then check in your submit event handler.

    Since you’re using old-style DOM0 event handler attributes, probably the hidden field fits better with what you’re doing:

    <form method="post">
     <input type="hidden" name="submitclicked" value="">
     <button type="submit" name="command" value="cancel" onclick="submitClick(this);">Cancel Order</button>
     <button type="submit" name="command" value="proceed" onclick="submitClick(this);">Save Order</button>
    </form>
    

    …where submitClick looks like this:

    function submitClick(button) {
        button.form.submitclicked.value = button.value;
    }
    

    …but I do recommend looking into using DOM2-style event handlers instead, or at least attaching DOM0 handlers in code blocks, as you can avoid creating global functions, share data without creating global variables, etc.


    Just to be clear, you don’t have to specify an onclick attribute on every element, the only reason I did that above is because you were using DOM0 handlers.

    The better way to handle it is with event bubbling. Since the click event bubbles up to the form from its descendant controls, including the submit buttons, you can hook the event on the form and then look to see if it occurred on a submit button and, if so, what that button’s value is.

    For instance, here with a DOM0 handler, attached dynamically to the form, which will alert the value of the submit button clicked:

    var form = document.getElementById("theForm");
    form.onclick = function(e) {
      // Get the event
      e = e || window.event;
    
      // Did it originate in an input[type=submit]?
      if (e.target.tagName === "INPUT" &&
          e.target.type === "submit") {
        // Yes
        alert(e.target.value);
      }
    };
    

    Live Example | Source

    Or using DOM2 handlers on any modern browser (not IE8 or earlier, but it would be easy to add attachEvent for those, which does much the same thing addEventListener does [and predates it]):

    document.getElementById("theForm").addEventListener("click", function(e) {
      // Did it originate in an input[type=submit]?
      if (e.target.tagName === "INPUT" &&
          e.target.type === "submit") {
        // Yes
        alert(e.target.value);
      }
    }, false);
    

    Live Example | Source

    Or using a library to make it easier (here it’s jQuery, but most of them have this feature, which is called event delegation):

    $("#theForm").delegate("input[type=submit]", "click", function() {
      alert(this.value);
      return false;
    });
    

    Live Example | Source (I’m using delegate there because I like the clarity; with recent versions of jQuery, you could use the hyper-overloaded on, but it’s less clear. If you choose to, note that the order of arguments is different.)

    The point here being that it’s not complicated, difficult, or particularly cumbersome.

    Re your ending question of your edit:

    …can anyone explain why there is no way to access this data directly before it is sent to the server?

    Probably not, no. It’s important to understand that a lot of this stuff just evolved. The submit button’s value being sent with the form is an HTML thing, apparently when doing the DOM HTML forms module, it just didn’t occur to anyone to say “Hey, we should have a property on the form that only exists during the form submission event that tells you what submitted the form.” It probably should have occurred to someone, but apparently, it didn’t.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this simple example code: <form name=text id=frm1 method=post> <input type=checkbox name=name[] value=1000> Chk1<br>
Consider simple multiple select box <select name=waste id=waste multiple> <option value=5>garbage</option> <option value=6>food</option> <option
Consider a simple .NET form with a couple of radio buttons and a checkbox.
Please consider the following HTML: <input type='text' id='name'/> <option id='option'> <select value='1'>hi</select> <select value='2'>bye</select>
Consider this simple example - public class Person { private String name; private Date
What should i consider when switching a simple(user+pass) login form from http to https?
I know about LOGIN_REDIRECT_URL and I know about <form action=?next={{ next|default:/foo }} method=post> in
Consider the following simple WinForms form with a textbox and a webbrowser control. Whenever
I have a form and two command buttons. I want that on pressing the
Consider the following jsf page: <h:body> <h:form id=SessionStartDialog> <table> <tr> <td class=label> <h:outputLabel value=Enter

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.