Martin C
Martin C

Reputation: 395

Form submitting using Javascript not working

I am using multiple form in a page here is my HTML code:

<form name="myform" id="myform" method="post" action="test.php">
<a href="javascript: submitform();" >submit</a>
<input type="hidden" name="encKey" value="12" />
</form>

<form name="myform" id="myform" method="post" action="test.php">
<a href="javascript: submitform();" >submit</a>
<input type="hidden" name="encKey" value="12" />
</form>


<form name="myform" id="myform" method="post" action="test.php">
<a href="javascript: submitform();" >submit</a>
<input type="hidden" name="encKey" value="12" />
</form>

And here is my javascript code:

<script type="text/javascript">
function submitform()
{
  document.myform.submit();
}
</script>

When i click on submit link, form is not submitting and gererate error in backend:

TypeError: document.myform.submit is not a function.

What may be issue?

Thanks

Upvotes: 0

Views: 1333

Answers (5)

fancy_guo
fancy_guo

Reputation: 1

the id of a form must be unique, try to change the form id to only name.

you can try to use

document.getElementById('formId').submit()
to submit the information of a form.

Upvotes: 0

maxime1992
maxime1992

Reputation: 23793

First of all you've got 3 id which are the same (myform), an id has to be unique.

Upvotes: 0

Fotis_zzz
Fotis_zzz

Reputation: 148

You can also use document.forms["myForm"].submit();. In this case "myForm" is the name of each form and it has to be unique as well as id.

Upvotes: 0

Yanki Twizzy
Yanki Twizzy

Reputation: 8001

I think you should use unique ids for all the forms. Then you can have a function that takes as a parameter the id of the form that you want to submit and then submit it. So you can have something like

<script type="text/javascript">
function submitform(formid)
{
  document.getElementById(formid).submit();
}
</script>

Upvotes: 1

Nikunj K.
Nikunj K.

Reputation: 9199

You may try this

<script type="text/javascript">
function submitform()
{
  document.getElementById('myform').submit();
}
</script>

Upvotes: 0

Related Questions