简体   繁体   中英

Submit iframe forms with button outside of iframe

i have contactus.html which has iframe <iframe name="myframe" id="frame1" src="contact_inputs.php"></iframe> containt only three inputs in contact_inputs.php

and the button submit in contactus.html i want to submit the forms in iframe file contact_inputs.php using the button outside of iframe to submit to php_form.php

contactus.html

   <iframe name="myframe" id="frame1" src="contact_inputs.php"></iframe>
   <form action="php_form.php" method="post">
   <input type="submit" name="DoIt" value="submit">
   </form>

contact_inputs.php

<form>

<input type="" name="cn">
<input type="" name="ex">
<input type="" name="cr">

</form>

Here you can try this logic:

 function sub(ev) { ev.preventDefault(); let frame1 = document.getElementById("frame1"); let parser = new DOMParser(); let doc = parser.parseFromString(frame1.innerHTML, "text/html"); let iframeForm = doc.body.querySelector("form"); console.log(iframeForm); //access value of form input field via name also console.log(iframeForm.cn.value); }
 <,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width. initial-scale=1.0" /> <title>Document</title> </head> <body> <iframe name="myframe" id="frame1" src="contact_inputs.php"> <form id="iframe1Form"> <input type="" name="cn" value ="HELLO"/> <input type="" name="ex" /> <input type="" name="cr" /> </form> </iframe> <form onsubmit="sub(event)"> <input type="submit" name="DoIt" value="submit" /> </form> </body> </html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM