12

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?

georgeawg
  • 46,994
  • 13
  • 63
  • 85
misaizdaleka
  • 1,706
  • 3
  • 21
  • 32
  • 1
    Dont use http without SSL to do that. Sending passwords over unprotected networks is never desired. – iankit Jun 12 '13 at 11:40
  • 1
    possible duplicate of [How do you post to an iframe?](http://stackoverflow.com/questions/168455/how-do-you-post-to-an-iframe) – bob esponja Mar 14 '14 at 10:11

1 Answers1

29

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>
Julian
  • 26,655
  • 14
  • 92
  • 132
bobince
  • 498,320
  • 101
  • 621
  • 807
  • I've tried it, but it's not working. It just opens the URL "http://www.example.com/login/index.php" in a new window and I am still not logged in. Of course, instead of two "guest", I put my credentials. – misaizdaleka Sep 27 '10 at 12:00
  • Works for me against moodle.org. If it's opened in a new window you have omitted the named `iframe`. – bobince Sep 27 '10 at 12:09
  • As strange as it may appear, I had an issue with `name="toframe"`. Changing it to `name="frame"` worked. So maybe @misaizdaleka had such an issue with `name="iframe"` (it might be a reserved name...). – Yvan Sep 13 '17 at 11:58