我需要PHP-jquery解决方案来填充一个基于选择的组合框在另一个组合框。我有组合框填满了国家,根据国家选择,我需要填写另一个组合框与所选国家的城市。我的代码看起来像这样(不工作):
HTML部分:<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>
PHP:
$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>';
}
和jQuery部分:
$(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成功传递给jQuery函数。在调用PHP并将国家代码传输到PHP并返回结果时出了问题。请帮助。
首先需要城市表,创建<div id='cities'></div>
..然后使用javascript从表格城市调用城市名称,而组合框国家是OnChange..
<select name='country' OnChange='stuff()'>
之类的…