I have run into a problem with something that should be simple. I want to submit a form programmatically using .submit(). It's a fairly standard simple form, four text inputs and three radio buttons, nothing at all unusual.:
<form action="register.php" method="post" accept-charset="utf-8" id = "signup_form">
Neither work. The strange thing is if I try to respond to the submit event on the form like this :
And then manually submit the form by clicking it does not stop it, the form will submit without interuption. I can however stop it if listen form the submit button click event... It seems like there is something wrong with the form submit event, I can't fire it programmatically and it doesn't seem to fire when I manually submit the form. I'm not sure whats wrong. There is another form on the same page but it has a different ID so I can't see it interfering. I also have tried changing the id of the form, just to test it, and that didn't change anything. Has anyone seen behavior like this before? Or have any suggestions on what could cause this? Any help is really appreciated. Thanks much!
Ok so I have done some more troubleshooting, I can now respond to the submit event, the earlier problem was because of a syntax error in some code unrelated to this, I missed it somehow,guess my brain is slow today.
I am still having problems with this however. Now I can see that I can programmatically trigger the submit event because if I attach a function to respond to it I can see the result. For example if I make a test div :
<div id = "test">HEY CLICK ME </div>
and attach a function that listens for the click event and then programmatically fires the submit event on the form :
and attach a listener for the form submit event :
I will see the alert but the form doesn't actually submit to the server. Could anyone enlighten me as to why this is happening? Thanks much!
with what you have posted so far, it should be submitting. there is nothing syntax-related wrong with your code that i can see. I would suggest looking at the script this is submitting to and seeing if it is working properly.
Thanks for the reply. The server side script works fine. If I submit by manually clicking the button or hitting return the form submits and everything goes as expected. Its when I use .submit() to try and make the form submit that it doesn't work. It's so strange, I can use an alert to respond to the submit event and that fires but the page doesn't do anything. I've done this a few times without problem and the fact that the submit event seems to happen makes it even more confusing. I'm tried to cut things out, I've isolated this code in the event that other code was interfering in some way and it is still happening. I am not sure what to try next .
Forms and their child elements should not use input names or ids that conflict with properties of a form, such as submit, length, or method. Name conflicts can cause confusing failures. For a complete list of rules and to check your markup for these problems, see DOMLint.
Indeed, the presence of the regular form submit button (which had the name "submit") was preventing the second submit button (which is actually a div and had no name) from working. I had tried deleting the original submit butotn, but to make things more confusing, the guy who originally coded this ALSO used the name "submit" on his cancel button.
For those happening by here with the same problem I had, when there is an item in a form with the name "submit", the method form.submit() breaks (it tries to use the item named submit as a function).