logan
logan

Reputation: 8346

Javascript Auto Form submission not working

I have following simple HTML Form & i tried to submit the form automatically during page load. Below Javascript code is not automatically submitting the form.

HTML :

<form action="SSL.php" method="POST" name="TForm" id="transactionForm">
<input type="hidden" name="merchantTxnId" id="merchantTxnId" value="test">
<input type="submit" name="submit" id="submit" value="submit" style="visibility:hidden">
</form>
Redirecting ... Please wait...

Java script:

<script>
    window.onload = function(){
        alert(1); // this is working..
            document.getElementById("transactionForm").submit(); //nothing is happening with this line . form is not getting submitted
    }
</script>

I found following error in Chrome console mode says: enter image description here Kindly suggest me where the problem is...

Upvotes: 2

Views: 558

Answers (1)

Matteo B.
Matteo B.

Reputation: 4064

You may not use submit as the name or id of any of your form elements.

The reason is, that you can reach each child of your form via document.getElementById('form').nameOfTheChild where nameOfTheChild is the name of the child. If you have a child with the name submit, document.getElementById('form').submit is a shortcut to address that child.

The documentation of .submit() says that :

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.

Upvotes: 5

Related Questions