RLJ Posted December 6, 2010 Share Posted December 6, 2010 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! Quote Link to comment Share on other sites More sharing options...
mikecampbell Posted December 6, 2010 Share Posted December 6, 2010 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> Quote Link to comment Share on other sites More sharing options...
RLJ Posted December 10, 2010 Author Share Posted December 10, 2010 Cheers! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.