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?
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).
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.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With