Ajax多个onchange动态依赖选择框

时间:2018-04-17 18:13:59

标签: php html mysql ajax

非常感谢您对以下问题的帮助。我有3个动态依赖下拉列表。方案1在以下情况下工作:单击下拉列表(Dd)1 =>值在Dd2中更改=>值在Dd3中更改。但我需要Dd3也依赖于Dd1& DD2。 我有ajax代码并假设它有错误:

 $(document).ready(function(){
 $('#country', '#state').on('change',function(){
 var countryID = $('#country').val();
 var stateID = $('#state').val();
 if(countryID, stateID){
 $.ajax({
 type:'POST',
 url:'search_city.php',
 data: "country_id="+countryID+"&state_id="+stateID,
 success:function(html){
 $('#city').html(html);
 }
 });

 }else{

 $('#city').html('<option value="">Select state first</option>');

 }
 })
 });

添加PHP文件:

  <?php 
  include('connect.php');  

  if (isset($_POST["country_id"] || isset($_POST["state_id"]))) {

  $query = $conn->query("SELECT * FROM city WHERE country_id = 
  ".$_POST['country_id']." AND state_id = ".$_POST['state_id']." ORDER BY 
  city_name");

  $rowCount = $query->num_rows;


  if($rowCount > 0){
    echo '<option value="">Select city</option>';
   while($row = $query->fetch_assoc()){
    echo '<option 
    value="'.$row['city_id'].'">'.$row['city_name'].'</option>';
    }
    }else{

   echo '<option value="">City not available</option>';
   }
   }


   ?>

0 个答案:

没有答案