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.

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

4 Comments

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.
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() )
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?
take a look at here stackoverflow.com/questions/1284535/php-transliteration let me know if that's works...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.