2
votes

I need PHP-jquery solution for filling one combo box based on selection in another combo box. I have combo box filled with countries, based on country selection I need to fill another combo box with cities in selected country. My code looks like this (doesn't work):

HTML part:

<select id="txt_country" name="txt_country" placeholder="" class="form-control">
<?php
    while( $country = $mydb->getRows() )
    {
 echo '<option  
 value="'.$country['country_code'].'">'.$country['country_name'].'</option>';
   }
   $mydb->closeConnection();
?>  
</select>

<select id="txt_city" name="txt_city" placeholder="" class="form-control">
</select>

The PHP part:

$cc = $_POST['txt_country']; //get country code from javascript

$mycdb = new mysqldatabase();

$mycdb->newConnection($config['db_host'], $config['db_user'], $config['db_pass'], $config['db_name']);

$getcitiesSQL = "SELECT * FROM city where population<>'' AND country_code='".$cc."'";
$mycdb->executeQuery( $getcitiesSQL );

while( $cities = $mydb->getRows() )
{
  echo '<option>'.$cities['accent_city'].'</option>';
}

And the jQuery part:

    $(function(){
        $('#txt_country').change(function(){
            //var ccode = $("#txt_country").val(); 
            //tried to send ccode as query string parameter
            $.get("getcities.php", { option : $("#txt_country").val() } , function(data) {
                $('#txt_city').html(data) ;
            } ) ;
        });
    });

CCode is successfully transmitted to jQuery function. Something goes wrong at calling PHP and transmitting country code to PHP and getting results back. Please help.

1

1 Answers

1
votes

first you need the cities table,, create <div id='cities'></div>.. and then use javascript to call city names from table cities while the combo box country is OnChange.. <select name='country' OnChange='stuff()'>

something like that...

idk how to explain it more easier...