Javascript Submit does not include Submit Button Value

Solution 1:

Yes, that is the correct behavior of HTMLFormElement.submit()

The reason your submit button value isn't sent is because HTML forms are designed so that they send the value of the submit button that was clicked (or otherwise activated). This allows for multiple submit buttons per form, such as a scenario where you'd want both "Preview" and a "Save" action.

Since you are programmatically submitting the form, there is no explicit user action on an individual submit button so nothing is sent.

Solution 2:

Using a version of jQuery 1.0 or greater:

$('input[type="submit"]').click();

I actually was working through the same problem when I stumbled upon this post. click() without any arguments fires a click event on whatever elements you select: http://api.jquery.com/click/

Solution 3:

Why not use the following instead?

<input type="hidden" name="submitDocUpdate" value="Save" />

Solution 4:

Understanding the behavior is good, but here's an answer with some code that solved my problem in jquery and php, that others could adapt. In reality this is stripped out of a more complex system that shows a bootstrap modal confirm when clicking the delete button.

TL;DR Have an input dressed up like a button. Upon click change it to a hidden input.

html

<input
    id="delete" 
    name="delete" 
    type="button" 
    class="btn btn-danger"
    data-confirm="Are you sure you want to delete?"
    value="Delete"></input>

jquery

$('#delete').click(function(ev) {
        button.attr('type', 'hidden');
        $('#form1').submit();
    return false;
});

php

if(isset($_POST["delete"])){
    $result = $foo->Delete();    
}