Click to See Complete Forum and Search --> : [RESOLVED] Dropdown from a Dropdown


fooley
08-15-2008, 05:33 AM
Hi everybody ,

I have 2 Dropdown :
-1 for Country and 1 for City

Table(ID,Country,City)

for the First Country i load it like this

<select name="Country">
<?php

$result = mysql_query("select distinct(Country) from pas");
while($row = mysql_fetch_row($result)){
?>
<option><?php echo $row[0]; ?></option>
<?php
}
?>
</select>

so the second dropdown will be filled by the city associeted with the country choosen in the first dropdown

<?php
if(isset($_POST['Country'])){
$result = mysql_query("select City from pas where Country='".$_POST['Country']."' ");
while($row = mysql_fetch_row($result)){
?>
<option><?php echo $row[0]; ?></option>
<?php
}

}

i try it this but not working

Thanks

thephorc
08-15-2008, 05:55 AM
i would use ajax to populate dropdown lists ..but if u wanna do it ur way, heres the code for it..


<?php

echo'<form method="post" action="/test.php"><div><select name="Country" onchange="submit();"><option value="">Please select a Country:</option>';

$a=mysql_query("SELECT `Country` FROM `pas` ORDER BY `Country` ASC");
while($b=mysql_fetch_assoc($a))
{
$select_country='';if(isset($_POST['Country'])){if($_POST['Country']==$b['Country']){$select_country=' selected="selected"';}}

echo'<option value="'.$b['Country'].'"'.$select_country.'>'.$b['Country'].'</option>';
}

echo'</select></div><div><select name="City">';

if(empty($_POST['Country']))
{
echo'<option value=""></option>';
}
else
{
$a=mysql_query("SELECT `City` FROM `pas` WHERE `Country` = '".$_POST['Country']."'");
while($b=mysql_fetch_assoc($a))
{
echo'<option value="'.$b['City'].'">'.$b['City'].'</option>';
}
}

echo'</select></div></form>';

?>


hope this helps;)

fooley
08-15-2008, 06:05 AM
Thanks m8 for your code
but can you check it again seem not to work for me

it would be nice to see it with Ajax

i have all in 1 page so action would be action=""

Thanks

thephorc
08-15-2008, 06:21 AM
i have all in 1 page so action would be action=""

yah, thats true ..but id still put the URL in.. maybe for security reasons? i dunno.. probably doesnt matter...

could u try it again ..cause i edited my post several times .. or whats the error? just a blank page?

fooley
08-15-2008, 06:23 AM
yes the first dropdown not filled blank

but with first code that i posted it's working

Thanks

fooley
08-15-2008, 06:27 AM
opps sorry working m8 now

if(isset($_POST['Country'])){if($_POST['Country']==$b['Country']){

Thanks m8 Great Thanks

fooley
08-15-2008, 06:29 AM
hmm still a bit problem

why the first dropdown , when i click on it i choose by example a country it back to first one when i choose it ?