smartcaveman
smartcaveman

Reputation: 42256

Can I use javascript/jquery to submit a non-local form in an iframe?

I am building a page at http://www.localurl.com.

I want to submit a POST form that is on http://www.url.com/form_step_1.html to http://www.url.com/form_step_2.html and display http://www.url.com/form_step_2.html in an iframe.

Is this possible to do with JavaScript/jQuery or does browser security prohibit this? Is there anyway it can be done with ajax?

In addition to the selected answer, I found this blog post extraordinarily informative on the subject.

Upvotes: 3

Views: 293

Answers (2)

Aaron Gibralter
Aaron Gibralter

Reputation: 4853

This isn't exactly what you want, but you can submit a form with its target set to the name of the iframe.

On foo.com:

<form action="http://bar.com/action" method="POST" target="baz">
  ...
</form>

<iframe name="baz" ...></iframe>

Upvotes: 1

gunwin
gunwin

Reputation: 4832

You cannot execute Javascript on a different domain that you do not have access to.

If you do have access then use:

document.domain = 'example.com'

on both pages. You can then submit a form within the iFrame with this:

window.top.myiframename.document.myformname.submit();

or

window.myiframename.document.myformname.submit();

NOTE: This probably won't work in IE8.

Alternatively, you could create your own page on your domain using php:

<?php
    echo file_get_contents("http://www.url.com");
?>

Open this page (which is on your local host) in your iframe and use:

    window.top.myiframename.document.myformname.submit();

or window.myiframename.document.myformname.submit();

Upvotes: 3

Related Questions