The autosubmit
document.myForm.submit();
Uncaught Error: document.myForm.submit();
function sendData()//Gets called once per second
{
document.body.insertAdjacentHTML("afterend",
'<form name=\"myForm\" action=\"http://www.mywebsite.com/formProcess.php\" method=\"POST\" target=\"submission.frame\">' +
'<input type=\"text\" name = "uuid" value=\"' + String(uuid) + '\">' +
'<input type=\"text\" name = "directionX" value=\"' + String(directionX) + '\">' +
'<input type=\"text\" name = "directionY" value=\"' + String(directionY) + '\">' +
'<iframe name=\"submission.frame\" hidden></iframe>' +
'</form>'
);
document.myForm.submit();
}
<?php
//Do bunch of stuff
?>
<form name="myForm" action="<?php echo $current_file; ?>" method="POST">
UUID <input type="text" name="uuid"> <br>
directionX <input type="text" name="directionX"><br>
directionY <input type="text" name="directionY"><br>
<input type="submit" value="Send">
</form>
The first time you call it, you add a form to the document and then document.myForm
is that form element.
The second time you call it, you add another form (with the same name!) to the document and then document.myForm
is an HTML Element Collection (which is an array-list object) containing two forms.
The collection doesn't have a submit
method. The two elements inside it do.
You could compensate for that by testing to see if there is a submit method and then grabbing the last element in the collection if it fails…
if (document.myForm.submit) {
document.myForm.submit();
} else {
document.myForm[document.myForm.length - 1].submit();
}
… but it really looks like you would be better off just switching to use XMLHttpRequest instead of throwing forms and iframes into the document. Those are what we used before Microsoft came up with XMLHttpRequest in the late 90s for the Outlook web app.