[ACCEPTED]-Form not submitting with JS-submit

Accepted answer
Score: 29

In short: change the id of your submit button 24 to something different than "submit". Also, don't 23 set the name to this value either.

Now, some 22 deeper insight. The general case is that 21 document.formname.submit is a method that, when called, will submit 20 the form. However, in your example, document.formname.submit is 19 not a method anymore, but the DOM node representing 18 the button.

This happens because elements 17 of a form are available as attributes of 16 its DOM node, via their name and id attributes. This 15 wording is a bit confusing, so here comes 14 an example:

<form name="example" id="example" action="/">
  <input type="text" name="exampleField" />
  <button type="button" name="submit" onclick="document.example.submit(); return false;">Submit</button>
</form>

On this example, document.forms.example.exampleField is a DOM node 13 representing the field with name "exampleField". You 12 can use JS to access its properties such 11 as its value: document.forms.example.exampleField.value.

However, on this example 10 there is an element of the form called "submit", and 9 this is the submit button, which can be 8 accessed with document.forms.example.submit. This overwrites the previous 7 value, which was the function that allows 6 you to submit the form.

EDIT:

If renaming the field 5 isn't good for you, there is another solution. Shortly 4 before writing this, I left the question 3 on the site and got a response in the form 2 of a neat JavaScript hack:

function hack() {
  var form = document.createElement("form");
  var myForm = document.example;
  form.submit.apply(myForm);
}

See How to reliably submit an HTML form with JavaScript? for complete 1 details

Score: 6

Given that your form has both an id and a 2 name defined, you could use either one of these:

With 1 the form tag's id:

document.getElementById('formname').submit();

With the form tag's name attribute:

document.forms['formname'].submit();
Score: 5

Try this:

fnSubmit()
{
  window.print();
  document.getElementById("formname").submit();
}

0

Score: 0

The most likely culprit is IE confusing 3 JavaScript variables, ids, and names. Search 2 in your source for something sharing the 1 name of your form.

Score: 0
  1. Place a input button inside your form.
  2. Give tabindex="-1" on it.
  3. Make It invisible using style="display:none;".

Like This

<input type="submit" tabindex="-1" style="display:none;" />

0

More Related questions