Submitting a form in an iframe using JavaScript

iframe is in the same domain, I tried the following code, but none of them worked:

  myframe.document.getElementById("myform").submit(); parent.top.frames[0].document.forms[0].submit(); myframe.document.getElementById("myform").submit(); MyIFrame = document.getElementById("myframe"); myIFrame.getElementById("myform").submit(); 

Update:

This is HTML:

 <html> <body> <iframe frameborder="0" scrolling="no" width="530" height="298" src="/iframe.php" name="myframe" id="myframe"> <p>iframes are not supported by your browser.</p> </iframe><br /> <form action="/styles.css" method="post" id="form1"> <input type="text" name="input1" value=""/> <input type="text" name="input2" value=""/> <input type="button" value="test" onclick="submitFrame()"> <input type="submit" value="submit"> </form> <script> function submitFrame(){ var MyIFrame = document.getElementById("myframe"); var MyIFrameDoc = (MyIFrame.contentWindow || MyIFrame.contentDocument); if (MyIFrameDoc.document) MyIFrameDoc = MyIFrameDoc.document; MyIFrameDoc.getElementById("myform").submit(); // ## error } </script> </body> </html> 

iframe.php:

 <form method="post" class="af-form-wrapper" id="myform" name="myform" action="/" > <input type="text" name="input1" value="2073809442" /> <input type="text" name="input2" value="1" /> <input type="submit" name="submit" value="submit" /> </form> 

Firebug says:

  MyIFrameDoc.getElementById("myform").submit is not a function 
+6
source share
3 answers

Try the following:

 var MyIFrame = document.getElementById("myframe"); var MyIFrameDoc = (MyIFrame.contentWindow || MyIFrame.contentDocument); if (MyIFrameDoc.document) MyIFrameDoc = MyIFrameDoc.document; MyIFrameDoc.getElementById("myform").submit(); 

UPDATE

I cannot understand why this does not work, but here is something that does:

 MyIFrameDoc.getElementById("mybutton").click(); 

iframe.php:

 <input type="submit" name="submit" value="submit" id="mybutton" /> 

UPDATE 2

The reason you get the submit is not a function error submit is not a function because you called your submit button submit , so MyIFrameDoc.getElementById("myform").submit actually refers to the HTMLInputElement method, not HTMLFormElement.submit() .

All you have to do is rename the submit button, for example:

 <input type="submit" name="submit2" value="submit" /> 
+11
source

Submit iframe url from javascript

  if (window.parent.$("#IframeId").length > 0) { window.parent.$("#IframeId")[0].contentDocument.forms[0].submit(); } 
+1
source

Thanks so much for your advice. I used JavaScript to validate some form information before submitting it. "MyIFrameDoc.getElementById (" mybutton "). Click ();" worked, but it ran into my submit button, which called the onclick () function. To work, I changed my original submit button to a button class with the onclick () function. Then I created a new submit button = "send" with id = "mybutton" with style = "width: 1px; height: 1px; border: 0;" essentially making the new submit button invisible and placing it above the button class with the onclick () function. The user presses the button button of the button, and the "onclick ()" script checks the information. If it is checked, the validation scripts call "MyIFrameDoc.getElementById (" mybutton "). Click ();" and submits the form.

-4
source

All Articles