Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending data through post method to an iframe

I have an iframe which is pointing to a moodle site. I need to pass to it my username and password, so that when the iframe is loaded, I am automatically logged in on moodle. So I have something like this:

<div id="iframe" style="width:787px; height:700px;">
    <iframe id="iframeCon" src ="http://www.somesite.net/moodle/login/index.php"
            width="100%" height="100%" frameborder="0">

    </iframe>
</div>

My question is how to send my username and password using POST method to this URL?

like image 676
misaizdaleka Avatar asked Sep 27 '10 11:09

misaizdaleka


People also ask

How do I provide data to an iframe?

The usual solution is to use window. parent in the frame to get "up" (into the document which contains the iframe tag). Now you can call any method in the container document. This method can manipulate the frame (for example call some JavaScript in the frame with the parameters you need).

Is iframe get or post?

The URL specified by the src attribute is used to make a GET request. You can get the results of a POST request to display in an iframe, but that requires that you submit a form with target="name_of_iframe" and not just use the src attribute.

Can you communicate with iframe?

All you have to do is first dispatch an event from the iframe to the parent that notifies the parent that the iframe is loaded (essentially a "ready message"). The parent will be listening for messages and if it receives the "ready message" event, it can then reply to the iframe with whatever message you want to send.

Are IFrames bad practice?

Iframes Bring Security Risks. If you create an iframe, your site becomes vulnerable to cross-site attacks. You may get a submittable malicious web form, phishing your users' personal data. A malicious user can run a plug-in.


1 Answers

To POST to an iframe you must use form target.

<form id="moodleform" target="iframe"
      method="post" action="http://www.example.com/login/index.php" >
    <input type="hidden" name="username" value="guest"/>
    <input type="hidden" name="password" value="guest"/>
    <input type="hidden" name="testcookies" value="1"/>
</form>
<iframe name="iframe"></iframe>
<script type="text/javascript">
    document.getElementById('moodleform').submit();
</script>
like image 165
bobince Avatar answered Oct 16 '22 15:10

bobince