[SOLVED] <iframe> question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Think Pink
Forum Contributor
Posts: 106
Joined: Mon Aug 02, 2004 3:29 pm

<iframe> question

Post by Think Pink »

hy. need some help pls.
i have index.php and page_2.php.

on index.php i have a dropdown (name=country) and a <iframe> with page_2.php loading in it. The page page_2.php has also a dropdown (name=cities).

When i choose a country in index.php, it loads in the dropdown from the <iframe > the corresponding cities.

The question is how I take selected value from dropdown loaded in the <iframe> and insert it in a $db.?

thx in advance
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

It depends. do you want your whole page to refresh when you insert the value or do you want your main page to remain while your iframe refreshes?
User avatar
Think Pink
Forum Contributor
Posts: 106
Joined: Mon Aug 02, 2004 3:29 pm

<iframe> question

Post by Think Pink »

my main page remains. Only the <iframe> refreshes. I use the <iframe> because is too complicated for me to make a dynamic dropdown with javascript, so the <iframe> size is not bigger than the drop down
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Put the drop down in a form and submit the form using the onChange javascript function. In the action of the form set it as follows:

Code: Select all

<form action="" method="post" name="form1" target="_self">
User avatar
Think Pink
Forum Contributor
Posts: 106
Joined: Mon Aug 02, 2004 3:29 pm

Post by Think Pink »

you mean like this?

index.php

Code: Select all

<?php
// connection information
?>

Code: Select all

&lt;table align="center" width="100" cellspacing="0" cellpadding="0" border="1"&gt;
&lt;form name="test" method="post" action="&lt;? $PHP_SELF; ?&gt;" &gt;
&lt;tr&gt;&lt;td width="100" align="center" valign="middle" height="35"&gt;
		&lt;select id="selCountry" name="selCountry" onChange="parent.content.location.href=iframe.php?selCountry='+document.getElementById('selCountry').value"&gt;&lt;option selected value="0"&gt;Select One&lt;/option&gt;

Code: Select all

<?php
$sql = "SELECT id_Country, nume_Country FROM Country ORDER BY nume_Country ASC";
				$resursa = mysql_query($sql);
					while($row = mysql_fetch_array($resursa))
						{
						print "<option value='".$row['id_Country']."'>".$row['nume_Country']."</option>";
						}
?>

Code: Select all

&lt;/select&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;tr&gt;&lt;td width="100" align="center" valign="middle" height="35"&gt;frame here
		&lt;iframe src="iframe.php" name="content" width="150" height="30" marginwidth="0" marginheight="0" hspace="4" vspace="4" align="middle" scrolling="no" frameborder="0"&gt;&lt;/iframe&gt;
&lt;/td&gt;&lt;/tr&gt;

&lt;tr&gt;&lt;td&gt;
		&lt;input type="text" name="message"&gt;	
&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;
	&lt;input type="submit" name="adaugaOG"&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/form&gt;
&lt;/table&gt;


&lt;table align="center" cellspacing="2" cellpadding="2" border="0"&gt;&lt;tr&gt;&lt;td align="center"&gt;

Code: Select all

<?php
if(isset($_POST['adaugaOG']))	{
$sqlOG = "SELECT * FROM oferta_generala";
$resultOG = mysql_query($sqlOG);
									
$sqlOG = "INSERT INTO ogTbl(id_Country, id_City, transport) VALUES ('".$_POST['selCountry']."', '".$_POST['selJudet']."', '".$_POST['transport']."')";
mysql_query($sqlOG);

print '<span class="mesaj4"><LI> Country a fost adaugata cu succes !</span>';
exit;
}
?>

Code: Select all

&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;


iframe.php

Code: Select all

<?php
// connection information
print "<select name="selJudet">"; 
<option selected value="">Alege City</option>
<?php
$id_Country = $_GET['selCountry'];
$sql = "SELECT id_City, nume_City FROM City WHERE id_Country = '".$id_Country."' ORDER BY nume_City ASC";
$resursa = mysql_query($sql);
	while($row = mysql_fetch_array($resursa))
		{
		print "<option value='".$row['id_City']."'>".$row['nume_City']."</option>";
		}
print "</select>";
?>
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

UNTESTED....

OK, I think I know what your trying to do.

Add a hidden field in the form of your main page:

Code: Select all

&lt;form name="test" method="post" action="&lt;? $PHP_SELF; ?&gt;" &gt; 
&lt;!-- HIDDEN FIELD --&gt;
&lt;input type="hidden" name="hidden_city"&gt;
Replace your current submit button whith this one:

Code: Select all

&lt;input type="submit" name="adaugaOG" onClick="submitForm();return false;"&gt;
Add the following Javascript function to the top of the main page:

Code: Select all

function submitForm()&#123;
	
document.test.hidden_city.value = parent.content.document.iframeform.selJudet.value;

document.test.submit();

&#125;
Replace your current IFRAME code with the following:

Code: Select all

<?php

echo '<form action="" method="get" name="iframeform">';
echo '<select name="selJudet">';
echo '<option selected value="">Alege City</option> ';

$id_Country = $_GET['selCountry']; 
$sql = "SELECT id_City, nume_City FROM City WHERE id_Country = '".$id_Country."' ORDER BY nume_City ASC"; 
$resursa = mysql_query($sql); 
   while($row = mysql_fetch_array($resursa)) 
      { 
      echo '<option value="'.$row['id_City'].'">'.$row['nume_City'].'</option>'; 
      } 
echo '</select>'; 
echo '</form>';
?>
This is untested but it should work. Also, I made some changes to how you print strings in php.

You can't print a string by doing the following:

Code: Select all

<?php
print "<select name="selJudet">"; 
?>
It should look like this:

Code: Select all

<?php
print "<select name="selJudet">"; 
//or
print '<select name="selJudet">'; 
//or
echo "<select name="selJudet">"; 
//or
echo '<select name="selJudet">'; 
//or
?>
User avatar
Think Pink
Forum Contributor
Posts: 106
Joined: Mon Aug 02, 2004 3:29 pm

<iframe> question

Post by Think Pink »

not working. now, not even the country name is aded to the $db.
I really don't get this. Some how I see the solution in my head, but it just doesnt work. And I have the same problem with double combos that are in the same page.

in dropdown 1 I chhose country, with an onchange event the page is submited, in dropdown 2 coresponded cities are shown. But now the problem comes with the first dropdown. After the page is submited (to load the second dropdown) the first option selected should be the country I just selected, but appears the first thing that is first in the list.

maybe you could help me with that, and i'll seee later about the other problem with the <iframe>.
User avatar
Think Pink
Forum Contributor
Posts: 106
Joined: Mon Aug 02, 2004 3:29 pm

Post by Think Pink »

ok, i made it.
i made a javascript tjhat takes the value from dropdown in the <iframe> and puts it in a hidden field on the form in main page.
Thx fro uyour help.
Post Reply