39

I have to send some parameter to an IFRAME with POST method. I have read here Setting the HTTP request type of an <iframe> that it isn't possible. I'm thinking a solution in Javascript but I can't implement it so I can't test if it is a valid solution for this issue. I want to ask if someone has the same problem and if it is possible to solve and in positive case how to?

1

4 Answers 4

60
<form ... target="hidden_iframe">
...
</form>

<iframe name="hidden_iframe" ...></iframe>
1
  • 1
    just make the form invisible and the iframe too.. and then do a form submit via js
    – Walialu
    Jul 18, 2011 at 9:04
31

How about using the target attribute of the form to point to iFrame?

 <form target="myIframe" action="http://localhost/post.php" method="post">
    <input type="hidden" value="someval" />
    <input type="submit">
</form>

<iFrame src="" name="myIframe"></iFrame>
2
  • 3
    What does post.php do? Why is this necessary?
    – Kalina
    Nov 13, 2013 at 18:03
  • 1
    @Kalina this is late obviously but it is the url that you want to send the post request to Dec 16, 2014 at 22:11
23

Just to give a concrete working example

<form id="loginForm" target="myFrame"  action="https://localhost/j_spring_security_check" method="POST">
 <input type="text" name="j_username" value="login" />
 <input type="text" name="j_password" value="password" />
 <input type="submit">
</form>

<iframe name="myFrame" src="#">
   Your browser does not support inline frames.
</iframe>

// Hide the form and do the submit 
<script>
 $(document).ready(function(){
 var loginform= document.getElementById("loginForm");
 loginform.style.display = "none";
 loginform.submit();
});
</script>
3
  • 2
    Using a hidden form and then submitting it does not makes sense. Why not just use an ajax request and populate the target div with the response received?
    – mobby
    May 14, 2018 at 11:49
  • haven't you try it? Oct 14, 2018 at 5:42
  • how can I add headers?
    – Dzak
    Oct 31, 2018 at 9:44
0

Proposed solution works almost fine in my case, problem is that my form submitting returns an authorization cookie. Website that is rendering iframe is located on different domain that iframe website and this method returns me an error that I can't set the cookies. Is there a secure way to fix this?

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.