Jump to content

Passing form data to PHP inside iframe


RLJ

Recommended Posts

Hi, I have a (probably quite simple) question about passing form data to a php script that contains an iframe with another php script that also needs some of this form data.

 

Basically, my problem is as follows:

 

I have the following code in a file called example1.php:

 

<html>
//some html stuff

<?php
if(isset($_POST['from'])) {$from1 = $_POST['from'];} 
else {$from1 = 'English';}
$langList = array('English', 'French', 'German', 'Dutch');

print('<select id="from" name="from">');
	foreach ($langList as $lang) {printf('<option %s>%s</option>', 
    					($from1 == $lang ? 'selected="selected"' : ''), $lang);
			     }
echo '</select>';
?>

//some more html stuff
</html>

 

As you can see, it retrieves a variable via POST and selects its value from a list. The variable is posted from an html file (example0.htm) containing the following:

 

<form method="post" action="example1.php">

 

This worked fine, until I put example1.php inside an iframe in example2.php as follows:

<html>
//some html here
<?php
//some php here
?>
<iframe src ='example1.php' id='something' name='something' >
</iframe>
</html>

and changed: "post" action="example2.php" in my html form.

 

My question is: how do I pass form data from example0.htm to example2.php to example1.php, so that example2.php gets loaded when the submit button on my form gets pressed, but so that example1.php (which gets loaded from the iframe in example2.php) can also access the form data?

 

Thanks!

Link to comment
Share on other sites

The problem is that the parameter is posted to example2.php and not example1.php. As always there are several solutions to this problem (eg, using the session, or using something other than an iframe), but the easiest thing to do would be to pass the post parameter through to example2.php in the query string. This means example1.php will read from $_GET, not $_POST.

 

In example2.php

 

<html>
//some html here
<?php
if(isset($_POST['from'])) {$from1 = $_POST['from'];} 
   else {$from1 = 'English';}
//some php here
?>
<iframe src ='example1.php?from=<?php echo $from1; ?>' id='something' name='something' >
</iframe>
</html>

 

In example1.php

 

<html>
//some html stuff

<?php
if(isset($_GET['from'])) {$from1 = $_GET['from'];} 
   else {$from1 = 'English';}
$langList = array('English', 'French', 'German', 'Dutch');

print('<select id="from" name="from">');
      foreach ($langList as $lang) {printf('<option %s>%s</option>', 
                   ($from1 == $lang ? 'selected="selected"' : ''), $lang);
                 }
   echo '</select>';
?>

//some more html stuff
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.