2

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.

Bogomil
  • 21
  • 1

1 Answers1

1

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...

J. Zend
  • 88
  • 7
  • Maybe this is not a good solution. Because the number of cities is too many. About 3 million total for all the countries. Can't put this in some table or array and than call with javascript. Need a solution with PHP extracting cities only for 1 country. I need to mention that this page that displays cities and countries is already a subpage called by jquery .load method. – Bogomil Jun 28 '16 at 05:09
  • There was an error in PHP script. Instead of POST should be GET: $cc = $_GET['txt_country']; and the variable to fetch the rows should be mycdb and not mydb while( $cities = $mycdb->getRows() ) – Bogomil Jun 28 '16 at 06:05
  • Hallelujah! It works! Problem was in PHP file. Solved it by myself. Now only problem is how to adjust the code page so that cities will be displayed without strange characters. For example for some French or Turkish or Russian cities or even Chinese. Any suggestions? – Bogomil Jun 28 '16 at 06:10
  • take a look at here http://stackoverflow.com/questions/1284535/php-transliteration let me know if that's works... – J. Zend Jun 28 '16 at 06:46