By rmanola on
In my module I allow the creation of a content, I need to submit the node form using Javascript/Jquery, since the Jquery submit() method don't work in it's version inside Drupal 5, I'm using the "submit()" call from the native javascript, when I want to submit the form, I try:
document.forms[0].submit();
At first it seems to work because the page is validated and if pass, it is "submited" and returns to the edit form page, I mean, the node isn't saved at all, it seems to be a submition that doesn't take effect, the page is just validated and reloaded, it returns the way it was.
Anyone know a way of truly submiting a node form page using Javascript? What am I missing here?
Comments
...
There are two problems here. First, you don't "click" the form's button. When a button isn't "clicked", its value isn't sent to the server, so FAPI (under most circumstances, including yours) doesn't call hook_submit(). The other problem is that, if you have another form on the page, e.g. the "search" form, then forms[0] may not apply to your node-editing form. Try
$('form#node-form input[@id=edit-submit]')[0].click()instead (I'm doing [@id=...] instead of #id because Drupal isn't fully HTML compliant and there might be several buttons with this ID, in which case getElementById fails).First, thanks for the
First, thanks for the reply.
I'm aware of the issues about using "document.forms[0]", I'm using that just for test purposes. Using this piece of code for referencing the submit button: "$('form#node-form input[@id=edit-submit]')[0]" doesn't work, if I take off the "[0]", it refers properly. I tried using the "click()" method in the submit button before and was either not working. Now I tried using the backward compability function of Jquery for JavaScript (get() function), so I did:
This way I call the "Click()" function from the Javascript, and not from the Jquery, doing that it worked fine, the node was submited :)
It's very sad that the Jquery used in Drupal 5 has these methods that just DON'T work. As fas I have tested, they are two:
submit(void) (http://docs.jquery.com/Events/submit)
click(void) (http://docs.jquery.com/Events/click)
I hope things like that are already fixed in Drupal 6, or be fixed in next versions of Drupal 5.
Thanks!
I killed most of an afternoon on a problem of which this was an integral part, and this thread was pretty much the only thing that steered me in the right direction. Neither $('#form-id').submit() nor $('#submit-button-id').click() worked, but these examples did.