0
<form action="/?wpmlmethod=offsite&amp;list=2&amp;wpmlformid=" method="post">
...
</form>

I tried:

<script type="text/javascript">document.forms[0].submit();</script>

But it didn't work.

2
  • when do you want to submit the form?
    – Sarfraz
    Commented May 17, 2010 at 9:35
  • /?wpmlmethod=offsite&list=2&wpmlformid=
    – Steven
    Commented May 17, 2010 at 9:38

2 Answers 2

2

You should submit it on some click event, etc, for example:

elem = document.getElementById('button_id');

elem.onclick = function(){
  document.forms[0].submit();
};

Update:

As you said form is already filled, you want to submit it straight away, you can try this instead:

window.onload = function(){
  document.forms[0].submit();
};

Note that forms[0] represents the first form on your page, if there are more than one forms on your page, you will need to specify the correct index for it eg forms[1], forms[2], etc

5
  • So I can't just submit a form without user interaction?
    – Steven
    Commented May 17, 2010 at 9:33
  • @Steven: You can submit, but user is suppossed to fill in the form, right?
    – Sarfraz
    Commented May 17, 2010 at 9:35
  • @Steven: When do you want the form to be submitted?
    – Sarfraz
    Commented May 17, 2010 at 9:36
  • The form is filled out already, however the app doesn't recognise it (not my app). So I was going to just resubmit it again.
    – Steven
    Commented May 17, 2010 at 9:36
  • action="/?wpmlmethod=offsite&amp;list=2&amp;wpmlformid="
    – Steven
    Commented May 17, 2010 at 9:37
1
  1. Make sure you call the submit method after the form exists (either by placing the script after the form or using an onload or onready event)
  2. Make sure you do not have a form control (e.g. an input) with the name or id of submit as this will clobber the submit method with a reference to that HTMLElementNode.

That said, you probably shouldn't be loading a page just to instantly submit a form with JS in the first place. You should have already had all the information needed on the server when you built the form. It smells of bad design and can break the back button.

2
  • Yeah this is just a quick hack, for an app I didn't write :(
    – Steven
    Commented May 17, 2010 at 9:35
  • Type is irrelevant, only the name and id matter for this.
    – Quentin
    Commented May 17, 2010 at 9:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.