submiting a form inside an iframe from outside the iframe

This one has me stumped (friday afternoon and I am done!).  I am certain it should be done (as long as the iframe content is from the same site).  Yet EVERY example I have found online doesn't work in Firefox (not sure about other browsers).  I have tried many possible variations.  I have other workarounds, but for various reasons I would really like to be able to just have a submit button outside the iframe, that when pressed submits the content of a form inside of the iframe.

Here is just one:

window.frames['myframe'].document.forms['suggestForm'].submit();

Ultimately I get an error saying that there is no submit function, or no properties, etc. 

Has anyone done this and can share their code?

TweetBacks
Comments
Hugo Matinho's Gravatar hey i got this working for you i think. how can i share the code with you ?
# Posted By Hugo Matinho | 9/22/07 12:29 PM
Joshua's Gravatar sweet, just copy/paste it into the comments if you don't mind, so others that search from google and see it too.
# Posted By Joshua | 9/22/07 12:51 PM
Hugo Matinho's Gravatar hey, here's your script it's divided in two parts, this one goes in between the head tags:
<script type="text/javascript">

/**
*
*   SubmitForm function definition:
*   Reference the iFrame: var yourVariable = parent.document.getElementById("iFrameNameHere");
*   Reference to the window object generated by the the iframe: var yourWindowVariable = yourVariable.contentWindow; //other browsers use contentDocument
*   Reference to the form object inside of the iFrame: var theFormObject = yourWindowVariable.document.getElementById("formNameHere");
*   Submit the form now :) theFormObject.submit();
*/

function SubmitForm(){
//make a reference to the iFrame;
var iframeElem = parent.document.getElementById("iFrame");
//make a reference to the window of the iFrame (Note: for Mozilla it's diferent you got to use contentDocument instead of contentWindow);
var win = iframeElem.contentWindow;
//make a reference to the form element in the iframe;
var theForm = win.document.getElementById("form1");
//submit the form
theForm.submit();
//just testing
alert("ok");
}

</script>



and this one goes into the end just before closing the body tag:

<script>
/*
*
*   Make it available in the DOM
*   Reference your submit button
*
*/

var myButton = parent.document.getElementById("submit");

/**
*
*   Older method of attaching event:
*   myButton.onclick = SubmitForm;
*
*/

/**
*
*   IE Hack - attachEvent instead of addEventListener for other browsers
*
*/
myButton.attachEvent('onclick', SubmitForm);
</script>


i've comented the javascript nothing two hard but i hope you understand it
# Posted By Hugo Matinho | 9/22/07 2:51 PM
Hugo Matinho's Gravatar by the way the properties i used here are for IE .. if you need them in firefox check the comments
# Posted By Hugo Matinho | 9/22/07 2:58 PM
Joshua Cyr's Gravatar Hi Hugo,

Thanks very much for posting this. Afraid I am still having the same error, in both IE and FF. I tried it in my own code, but also just used your code on a test page. To be clear, my submit button is on the parent frame, not in the iframe. For whatever reason I just can't get at the properties of the iframe form to submit. I get either an object expected error or a iframeElem has no properties error. Pitty.
# Posted By Joshua Cyr | 9/24/07 9:27 AM
m larsen's Gravatar have you tried explicitly setting the <tt>document.domain</tt> in both the parent and the iframe?
# Posted By m larsen | 9/28/07 1:30 PM
James's Gravatar Hey Dude...

Try this:

var iframe = window.frames[&lt;FRAMEID&gt;].document.forms[0].submit();

You have to remember though, the contents of your iframe need to be fully loaded for this to work.
# Posted By James | 9/20/08 5:03 PM
Peter's Gravatar I doubt you’re still having this problem over 2 years later, but if anyone else finds this page on the web…

That type of error “there is no submit function, or no properties, etc.” can happen when you have an input or button with the name “submit”—change the name to something other than “submit”.
# Posted By Peter | 2/7/10 3:21 PM

NAVIGATION

Home
About Me

RSS


Search

Subscribe

Enter your email address to subscribe to this blog.

Recent Entries

Inception Plot Questions
Random Chuck Norris Fact Generator With A Twist
Virtual Currency for Buses
Applying ColdFusion Security Patches Gotcha
Privacy, Walled Gardens, Standards and Our Future

Recent Comments

FireFox 3.6 KTML Editor Fix
rock guitar said: Is the ktml realy discontinued for dreamweaver [More]

FireFox 3.6 KTML Editor Fix
Joshua said: Hi Wayne, I just tried opening the editor in IE from the link you supplied. I was able to open the ... [More]

FireFox 3.6 KTML Editor Fix
wayne said: Hi everybody, thanks for your reply joshua. I still have a bug for ktml in asp for a link properties... [More]

Inception Plot Questions
said: [More]

Inception Plot Questions
Taylor said: Hey there Josh, Great questions all. You seem to think this is more of a Jacob's Ladder equivalent ... [More]

Calendar

Sun Mon Tue Wed Thu Fri Sat
   1234
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30   

Archives By Subject

blogs (31) [RSS]
books (4) [RSS]
Crazy (39) [RSS]
DIY (8) [RSS]
Flex (3) [RSS]
games (10) [RSS]
GRRR (13) [RSS]
Ideas (11) [RSS]
Local (14) [RSS]
LOLpics (2) [RSS]
money (9) [RSS]
music (3) [RSS]
Personal (27) [RSS]
Photos (8) [RSS]
Politics (8) [RSS]
Projects (22) [RSS]
Review (19) [RSS]
RPM (9) [RSS]
Spam (16) [RSS]
Technology (68) [RSS]
Testing (3) [RSS]
TV (15) [RSS]
video (32) [RSS]
Web Dev (224) [RSS]
World of Warcraft (16) [RSS]