Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Submit does not include Submit Button Value

Ok, this is less of a question than it is just for my information (because I can think of about 4 different work arounds that will make it work. But I have a form (nothing too special) but the submit button has a specific value associated with it.

<input type='submit' name='submitDocUpdate' value='Save'/> 

And when the form gets submitted I check for that name.

if(isset($_POST['submitDocUpdate'])){ //do stuff 

However, there is one time when I'm trying to submit the form via Javascript, rather than the submit button.

document.getElementById("myForm").submit(); 

Which is working fine, except 1 problem. When I look at the $_POST values that are submitted via the javascript method, it is not including the submitDocUpdate. I get all the other values of the form, but not the submit button value.

Like I said, I can think of a few ways to work around it (using a hidden variable, check isset on another form variable, etc) but I'm just wondering if this is the correct behavior of submit() because it seems less-intuitive to me. Thanks in advance.

like image 413
Joshua Cook Avatar asked Nov 10 '09 16:11

Joshua Cook


People also ask

Why the submit button is not working?

Sometimes the problem is caused by old versions of the Javascript files, cached by your browser and can be fixed by clearing the browser cache. You can use the browser console of your browser for debugging. After the Javascript error is fixed, the submit button will automatically be enabled.

Why does submit button say submit query?

If you don't set a value for a submit button it defaults to "Submit Query". I assume you are using an image for your submit button since you have the default value. If you want to fix it for IE8 add a text indent using CSS which will push the default value off the screen.

What does javascript submit () do?

submit() allows to initiate form sending from JavaScript. We can use it to dynamically create and send our own forms to server.


Video Answer


2 Answers

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/

like image 33
Zach Avatar answered Sep 23 '22 13:09

Zach


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.

like image 135
Peter Bailey Avatar answered Sep 19 '22 13:09

Peter Bailey