I have a HTML form
:
<form name="myForm" method="post" id="myForm" action="ACTION_URL_HERE">
<input type="hidden" name="txt1" id="txt1">
<input type="hidden" name="txt2" id="txt2">
<input type="hidden" name="txt3" id="txt3">
<input type="submit" name="submit" value="Submit">
</form>
<input type="button" value="Submit form" onclick="MyFunction();">
I am submitting the form after filling the values in 3 hidden fields in the form in following function.
function MyFunction()
{
var isValid="";
$.post('ServletURL',{action: "getData"}).done(function(data)
{
$.each(data, function(index, text)
{
if(index==0)
isValid=text;
if(isValid=="OK")
{
if(index==1)
$("#txt1").val(text);
if(index==2)
$("#txt2").val(text);
if(index==3)
{
$("#txt3").val(text);
alert("Before form submit...");
document.getElementById('myForm').submit();
alert("Form is submited.");
}
}
});
});
}
MyFunction() is working perfectly, except that it is not submitting the form. I can see the alert alert("Before form submit...");
, but can't see the second alert alert("Form is submited.");
.
Then I have removed following submit
button from the form which was unnecessary:
<input type="submit" name="submit" value="Submit">
Then the code is working fine, and form is getting submitted.
My question is that why form was not getting submitted when there was submit
element in the form?