weixin_33739646 2015-08-19 08:01 采纳率: 0%
浏览 10

可靠的选择框

I have this code :

<form id="frmKat" role="form" action="" method="post">
    Country :
    <select name="country" id="country">
      <option>-select your country-</option>
    <?php 
    $result=mysqli_query($kon, "SELECT * from regios");
    while($country=mysqli_fetch_assoc($result)){         
    echo "<option value=$country[id]>$country[naam]</option>";

    } ?>
    </select>

    City :
    <select name="city" id="city">
        <option>-select your city-</option>
    </select>
    <div style="clear:both;"></div>
    <button type="submit" class="pull-right btn btn-success" name="btnConvert" id="btnConvert">Convert</button>
</form>

My ajax code is :

<script type="text/javascript">
       $(document).ready(function(){


           $("#country").change(function(){
                 var country=$("#country").val();
                 $.ajax({
                    type:"post",
                    url:"getcutoff.php",
                    data:"country="+country,
                    success:function(data){
                          $("#city").html(data);
                    }
                 });
           });
       });
  </script>

And my getcutoff.php file is next :

 <?php
session_start();
include("config.php");
global $kon;
ob_start();
if(isset($_SESSION["admin"])){ 

  $country=$_POST["country"];

  $result=mysqli_query($kon, "select * FROM cutoffs WHERE regio_id=". $country ." ");
  while($city=mysqli_fetch_array($result)){
    echo "<option value=$city[id]>$city[datetime]</option>";

  }
}else{
    ob_flush();
    die("Salut");   
}
?>

I'm trying to make dependable selectboxes. When user chooses something in the first selected box, then they will be taken to the second one where they can choose values which are connected to the first selected box. In first box I get values, but when I choose something in the second one, I get nothing. Where am I making a mistake?

  • 写回答

2条回答 默认 最新

  • weixin_33743248 2015-08-19 08:16
    关注

    Group your options in a php variable as string and use a single echo in getcutoff.php

    $res ='';
    while($city=mysqli_fetch_array($result)){ $res.="<option value=$city[id]>$city[datetime]</option>"; } 
    echo $res;
    
    评论

报告相同问题?