Skip to content Skip to sidebar Skip to footer

Submitting A Form In An Iframe With Javascript

The iframe is on the same domain, I tried the following code, but none of it worked: myframe.document.getElementById('myform').submit(); parent.top.frames[0].document.forms[0].su

Solution 1:

Try this:

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

UPDATE

I can't figure out why this doesn't work, but here is something that does:

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

iframe.php:

<inputtype="submit" name="submit" value="submit"id="mybutton" />

UPDATE 2

The reason you're getting the submit is not a function error is because you've named your submit button submit, so MyIFrameDoc.getElementById("myform").submit actually references an HTMLInputElement, not the HTMLFormElement.submit() method.

All you need to do is rename your submit button, e.g.:

<inputtype="submit" name="submit2" value="submit" />

Solution 2:

Submit the Iframe's URL from javascript

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

Solution 3:

You do not need to post into iframe. You can use normal blank "visible" window. Define your form like this:

var request = document.createElement("FORM")
request.id = "request"
request.name = "request"
request.action = "callYour.php"
request.method = "POST"
request.target = "_blank"

and in your php, when you are done just close the window by:

window.close();

within the HTML part, or within embedded script tags. The new window temporarily opens up but closeses itself when it is completed.

Post a Comment for "Submitting A Form In An Iframe With Javascript"