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?