in Javascript

Submit form of iframe from parent window

In phpxperts yahoo group there was a thread about how to submit a form in a iframe outside the iframe I mean from parent window. I replied my idea using js but the iframe should be in same domain. The js code is like
[code language=”php”]window.top.myframename.document.myformname.submit();[/code]
let me give u whole code. make a new file named frame.html and write the bellow code in that file
[code language=”php”]
<form id="myformid" name="myformname" method="post" action="" target="_self" onSubmit="">
Name: <input type="text" size="20" name="name" value="" id="namefield" />
</form>
[/code]

and open a new file again and save in same dir with name index.html and write the bellow code in it
[code language=”php”]
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Iframe form submit from outside</title>
</head>

<body>

<div id="infodivid"></div>
<iframe src ="frame.html" width="auto" height="40px" frameborder="0" id="myframeid" name="myframename">
</iframe>
<p><input onClick="submitiframeform(); return false;" type="button" name="submit" value="Submit" /></p>
<script type="text/javascript">

function submitiframeform(){
window.top.myframename.document.myformname.submit();
}
</script>
</body>
</html>
[/code]

now open the index.html in browser and click the submit button and see the form is getting submitted. I think If you just check the code then I don’t need to explain ‘what is why’.

thanks

  1. Awesome explanation. that’s very much helpful

    I had a problem doing something above this.

    I had a text box inside the iframe which needs to be populated with the value of parent textbox(in the main window)

    can you able to post a solution. that will be great.

    Thanks in advance

  2. For starters, put the javascript between the <head> tags and not the body. Next, try document.iframename.formname.submit(), but you may have trouble with that. IE8, unlike IE7, is now being much more protective of scripting functions across iFrames for security purposes.

Comments are closed.