I am trying to create a dynamic drop down. What I want is a pull down that contains a list of countries that is queried from a table. Once the user selects a country, the next drop down will then contain all the regions that are associated with that country. I am quite familiar with PHP but not too much with Javascript. My code is posted below. Right now, the code just doesn't do anything. I have a list of the countries and when I select one, the fill_sub() function just doesn't do it's part.
If someone could please point out my errors, that would be so much help.
Here is the code:
<?PHP
include '/data/www/connect.include
';
?>
<HTML>
<HEAD>
<SCRIPT language="JavaScript">
<!--
function Fill_Sub()
{
var country = document.FormName.country;
var region = document.FormName.region;
if( country.options[country.se
lectedInde
x].value != 0 )
{
region.length = 0;
}
<?PHP
$query = "SELECT country FROM tblcountry order by country";
$result = odbc_exec($connect, $query);
while( $Row = odbc_fetch_array($Result) )
{
?>
if( country.options[country.se
lectedInde
x].text == "<?PHP echo $Row[Country]; ?>" )
{
<?PHP
$Query2 = "SELECT region FROM tblcountry WHERE country = '$Row[Country]'";
$Result2 = odbc_exec($connect, $Query2);
$ctr = 0;
While( $Row2 = odbc_fetch_array($Result2)
)
{
echo "region.options[$ctr] = new Option('$Row2[Region]')";
echo "region.options[$ctr].valu
e = '$Row2[Region]'";
$ctr++;
}
?>
}
<?PHP
}
?>
}
-->
</SCRIPT>
</HEAD>
<BODY>
<FORM name="FormName" method="POST" action="">
<TABLE>
<TR>
<TD>Country: <SELECT name="country" onchange="Fill_Sub();">
<option selected>
<?PHP
$query = "SELECT * FROM tblcountry ORDER BY Name";
$result = odbc_exec($connect, $query) or die ("Query Failed");
for ($i = 0; $i < odbc_num_rows( $result ); ++$i)
{
$line = odbc_fetch_array($result);
?>
<option><?PHP echo $line[Name];
}
?>
</SELECT></TD>
<TD>Region: <SELECT name="region">
<option selected>
<option>Select a country</SELECT></TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>
Thank you for your help in advance.
Start Free Trial