1

I have a page with a form, and in the middle of the form I placed an iframe with HTML inputs boxes. How can I grab their content using PHP $_POST? I tried to do it as I normally do with forms:

$iframe = $_POST["iframe"];

But it didn't grabbed it.

How can I grab the inputs value from the iframe?

Ron
  • 6,543
  • 4
  • 23
  • 29

2 Answers2

0

The content in the iframe is a different webpage. You need to post the form from the iframe for php to access it.

If you need all the data from the iframe and main page at the same time, you would probably need to use javascript to either:

1) Submit both forms and handle the split serverside 2) Read the values from the iframe and submit as part of the parent page

Jonno
  • 789
  • 9
  • 26
  • Your second solution with JavaScript seems applicable. Do you have any idea how can I do this? – Ron Aug 11 '11 at 11:49
  • Your 2nd will be the solution I think. – scube Aug 11 '11 at 11:49
  • 1
    @Ron - Look at http://stackoverflow.com/questions/478078/accessing-a-form-that-is-in-an-iframe – Jonno Aug 11 '11 at 12:21
  • @Ron you said they that the iframe is on a different domain in another comment. This makes it much more complex. It's a hack, but you could get all the input on your own page, and then simulate a user sending it to the second system. However this is hacky. – Jonno Aug 11 '11 at 12:23
0

This won't work, unless the iframe is POSTing to your PHP script. The browser won't send data contained in an <iframe> to your script if the form that's being submitted is in the parent page.

One workaround is to use JavaScript to extract data from the iframe when the form is submitted. Then dynamically attach that data to your POST request.

The iframe's window object can be accessed like so:

var iframe = document.getElementById(iFrameName).contentWindow;

Note that this will only work if the source of the iframe is on the same domain as the parent page (for security reasons).

tskuzzy
  • 35,812
  • 14
  • 73
  • 140
  • Unfortunately, the iframe is different from the parent page. It's a whole different system -- that's why I'm using iframe. – Ron Aug 11 '11 at 12:02
  • Then I'm afraid you're out of luck :/. Modern browsers will block access to the iframe from the parent page if they're on a different domain. This is to protect against XSS attacks. – tskuzzy Aug 11 '11 at 12:04